Java File Upload (multiple files per file) and deletion, java File Upload
/**
* File Upload-single file
*
* @ Param request
* @ Param response
* @ Param path
* File storage path (path is the content after WebApp)
* @ 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 {
// Get the name of the uploaded file
String filename = mFile. getOriginalFilename ();
// Get the file suffix
If (filename! = Null &&! (""). Equals (filename )){
String fileExt = filename. substring (filename. lastIndexOf ("."));
// Regenerate the file name in time format
String newFileName = sdf. format (new Date ())
+ (Int) (Math. random () * 100) + fileExt;
FilePath = path + "/" + newFileName;
// Obtain the physical path of the upload Server
Path = request. getSession (). getServletContext ()
. GetRealPath ("\" + path );
// Write the file stream to the server
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 \ content)
* @ 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> ();
// Obtain 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 );
// Get the name of the uploaded file
String filename = mFile. getOriginalFilename ();
If (filename = "" | filename = null ){
Continue;
}
SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMddHHmmssSSS ");
// Get the file suffix
String fileExt = filename. substring (filename. lastIndexOf ("."));
// Regenerate the file name in time format
String newFileName = sdf. format (new Date ())
+ (Int) (Math. random () * 100) + fileExt;
String filePath = path + "/" + newFileName;
// Write the file stream to the server
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;
}
/**
* Deleting an object,
*
* @ Param request
* @ Param filePath: file path
* (Static/upload /...)
* @ Return
*/
Public static boolean fileDelete (HttpServletRequest request, String filePath ){
String fileUrl = request. getSession (). getServletContext ()
. GetRealPath ("\" + filePath); // obtain the physical path of the upload Server
File file = new File (fileUrl );
FileDelete (file );
Return false;
}