java file upload (single file multiple file) and delete

  • 2020-06-03 06:21:32
  • OfStack

Without further ado, look at the code


/**
*  File upload -- A single file 
* 
* @param request
* @param response
* @param path
*  File storage path ( path for WebApp\ Later) 
* @return
*/
public final static String fileUpload(HttpServletRequest request,
HttpServletResponse response, String path) {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
MultipartFile mFile = null;
for (Iterator<?> i = fileMap.keySet().iterator(); i.hasNext();) {
Object obj = i.next();
mFile = (MultipartFile) fileMap.get(obj);
}
String filePath = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
try {
//  Gets the file name of the uploaded file 
String filename = mFile.getOriginalFilename();
//  Gets the file suffix name 
if (filename != null && !("").equals(filename)) {
String fileExt = filename.substring(filename.lastIndexOf("."));
//  Regenerate filenames in a time format 
String newFileName = sdf.format(new Date())
+ (int) (Math.random() * 100) + fileExt;
filePath = path + "/" + newFileName;
//  Gets the physical path of the upload server 
path = request.getSession().getServletContext()
.getRealPath("\\" + path);
//  The file flow is written to the server side 
File saveFile = new File(path, newFileName);
FileCopyUtils.copy(mFile.getBytes(), saveFile);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return filePath;
}
/**
*  File upload -- Multiple files 
* 
* @param request
* @param response
* @param filePaths
*  ( fileinputId . WebApp\ Later) 
* @return
*/
public final static Map<String, Object> fileUploads(
HttpServletRequest request, HttpServletResponse response,
String path) {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
Map<String, Object> filePaths = new HashMap<String, Object>();
//  Gets the physical path of the upload server 
String fileUrl = request.getSession().getServletContext()
.getRealPath("\\" + path);
for (Iterator<?> i = fileMap.keySet().iterator(); i.hasNext();) {
Object obj = i.next();
MultipartFile mFile = (MultipartFile) fileMap.get(obj);
//  Gets the file name of the uploaded file 
String filename = mFile.getOriginalFilename();
if (filename == "" || filename == null) {
continue;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
//  Gets the file suffix name 
String fileExt = filename.substring(filename.lastIndexOf("."));
//  Regenerate filenames in a time format 
String newFileName = sdf.format(new Date())
+ (int) (Math.random() * 100) + fileExt;
String filePath = path + "/" + newFileName;
//  The file flow is written to the server side 
try {
filePaths.put(obj.toString(), filePath);
File saveFile = new File(fileUrl, newFileName);
FileCopyUtils.copy(mFile.getBytes(), saveFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return filePaths;
}
/**
*  Delete the file, 
* 
* @param request request 
* @param filePath The file path 
* (static/upload/...)
* @return
*/
public static boolean fileDelete(HttpServletRequest request, String filePath) {
String fileUrl = request.getSession().getServletContext()
.getRealPath("\\" + filePath);//  Gets the physical path of the upload server 
File file = new File(fileUrl);
fileDelete(file);
return false;
}

Related articles: