SpringMVC multi-File Upload instance
Background Conroller
Import java. io. file; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. IOException; import java. io. printWriter; import java. io. unsupportedEncodingException; import java.net. URLDecoder; import java. util. date; import java. util. iterator; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import javax. swing. filechooser. fileNameExtensionFilter; import org. springframework. stereotype. controller; import org. springframework. web. bind. annotation. requestMapping; import org. springframework. web. bind. annotation. requestMethod; import org. springframework. web. bind. annotation. requestParam; import org. springframework. web. multipart. multipartFile; import org. springframework. web. multipart. multipartHttpServletRequest; import org. springframework. web. multipart. commons. commonsMultipartFile; import org. springframework. web. multipart. commons. commonsMultipartResolver; import org. springframework. web. servlet. modelAndView; import com. tgb. web. controller. entity. user; @ Controller @ RequestMapping ("/file") public class UploadController {@ RequestMapping ("/upload") public String addUser (@ RequestParam ("file") CommonsMultipartFile [] files, httpServletRequest request) {for (int I = 0; I
"+ Files [I]. getOriginalFilename (); if (! Files [I]. isEmpty () {int pre = (int) System. currentTimeMillis (); try {// get the output stream and rename the uploaded file FileOutputStream OS = new FileOutputStream ("H:/" + new Date (). getTime () + files [I]. getOriginalFilename (); // get the input stream FileInputStream in = (FileInputStream) files [I]. getInputStream (); // write the file int B = 0 in bytes; while (B = in. read ())! =-1) {OS. write (B);} OS. flush (); OS. close (); in. close (); int finaltime = (int) System. currentTimeMillis (); System. out. println (finaltime-pre);} catch (Exception e) {e. printStackTrace (); System. out. println ("Upload error") ;}}return "/success" ;}@ RequestMapping ("/upload2") public String upload2 (HttpServletRequest request, HttpServletResponse response) throws IllegalStateException, IOException {// create a common multi-part parser CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver (request. getSession (). getServletContext (); // determines whether a file is uploaded in the request, that is, if (multipartResolver. isMultipart (request) {// convert to multipart request MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; // retrieve all the file names in the request Iterator
Iter = multiRequest. getFileNames (); while (iter. hasNext () {// record the start time of the upload process, used to calculate the upload time int pre = (int) System. currentTimeMillis (); // get the uploaded file MultipartFile = multiRequest. getFile (iter. next (); if (file! = Null) {// get the name of the currently uploaded file String myFileName = file. getOriginalFilename (); // if the name is not "", the file exists; otherwise, the file does not exist if (myFileName. trim ()! = "") {System. out. println (myFileName); // rename the uploaded file name String fileName = "demoUpload" + file. getOriginalFilename (); // defines the upload path String path = "H:/" + fileName; File localFile = new File (path); file. transferTo (localFile) ;}// record the time after the file is uploaded int finaltime = (int) System. currentTimeMillis (); System. out. println (finaltime-pre) ;}} return "/success" ;}@ RequestMapping ("/toUpload") public String toUpload () {return "/upload ";}}
Front-end page
<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <Script type = "text/javascript" src = "/js/
"> </Script>
Insert title here<Script type = "text/javascript"> I = 1; j = 1; $ (document ). ready (function () {$ ("# btn_add1 "). click (function () {document. getElementById ("newUpload1 "). innerHTML + ='
'; I = I + 1 ;}); $ ("# btn_add2"). click (function () {document. getElementById ("newUpload2"). innerHTML + ='
'; J = j + 1 ;}); function del_1 (o) {document. getElementById ("newUpload1 "). removeChild (document. getElementById ("div _" + o);} function del_2 (o) {document. getElementById ("newUpload2 "). removeChild (document. getElementById ("div _" + o) ;}</script> SpringMVC byte stream input uploads files
SpringMVC packaging class uploads files