Java File Upload (multiple files per file) and deletion, java File Upload

Source: Internet
Author: User

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;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.