Simple implementation of spring MVC multi-file upload and spring MVC File Upload

Source: Internet
Author: User

Simple implementation of spring MVC multi-file upload and spring MVC File Upload

Public ResponseItem uploadFile (MultipartHttpServletRequest request, FileItem fileItem, PageData pd ){
ResponseItem responseItem = new ResponseItem ();
Try {
Boolean isSuccess = false;
MultiValueMap <String, MultipartFile> multiMap = request. getMultiFileMap ();
String relativePath = CreateDateDirUtil. create (Const. UPLOADPATH + "/upload"); // Save the folder
Set <String> keys = multiMap. keySet (); // traverses the file
IsSuccess = true;
If (isSuccess ){
FileItem uploadfile = new FileItem ();
For (String key: keys ){
List <MultipartFile> mutiFiles = multiMap. get (key );
For (MultipartFile file: mutiFiles ){
String uuid = UuidUtil. get32UUID ();
String suffix = file. getOriginalFilename (). substring (file. getOriginalFilename (). indexOf ("."); // obtain the file type
Uploadfile. setProc (fileItem. getProc (); // set the process instance ID
Uploadfile. setFileTempname (uuid + suffix); // set the database file storage name, 32-bit
Uploadfile. setNames (fileItem. getNames (); // you can specify the name of the uploader.
Uploadfile. setUserid (fileItem. getUserid (); // you can specify the upload id.
Logger. debug ("=>" + suffix );
String fileName = URLDecoder. decode (file. getOriginalFilename ());

Logger. debug ("Upload attachment name =>" + fileName );
Uploadfile. setFilename (fileName); // upload the attachment name
Uploadfile. setFilepath ("upload/" + relativePath );
String type = fileItem. getType ();
If (type. equals ("blfj ")){
FileItem. setType ("apply for attachments ");
}
If (type. equals ("jxfj ")){
FileItem. setType ("end item attachment ");
}
If (type. equals ("xcfj ")){
FileItem. setType ("Inspection attachment ");
}
Uploadfile. setType ("apply for attachments ");
Uploadfile. setTime (new Date (); // upload time
Logger. debug ("=>" + Const. UPLOADPATH + "/" + relativePath + uuid + suffix );
File. transferTo (new File (Const. UPLOADPATH + "/upload/" + relativePath, uuid + suffix); // Write file, File name produced by UUID
/** Insert a media file */
IsSuccess = workflowDao. insertWorkflowUploadFile (uploadfile );
}
}
If (isSuccess ){

Return responseItem. getSuccessInfo (isSuccess );
} Else {
Return responseItem. getFailureInfo ("false ");
}
}
} Catch (Exception e ){

E. printStackTrace ();
}

Return responseItem;
}

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.