Not much to say, directly on the instance code!!!
I'm using the idea tool, MAVEN management.
First, the package required to upload the file is introduced in Pom.xml.
<!--fileupload--> <dependency> <groupId>commons-fileupload</groupId> < artifactid>commons-fileupload</artifactid> <version>1.3.1</version> </ dependency> <dependency> <groupId>commons-io</groupId> <artifactId> commons-io</artifactid> <version>2.2</version> </dependency>
Single File Upload:
/*** Created by Chen on 2017/9/17.*/@Controller @requestmapping ("Home") Public classUploadcontroller {@RequestMapping ("/oneupload") PublicString Upload (@RequestParam ("File") multipartfile file, HttpServletRequest request) {String Uploadurl= Request.getsession (). Getservletcontext (). Getrealpath ("/") + "upload/"; String filename=File.getoriginalfilename (); //determines whether the corresponding path exists, does not exist, createsFile dir =NewFile (Uploadurl); if(!dir.exists ()) {Dir.mkdir (); } System.out.println ("File Upload:" + Uploadurl +filename); File TargetFile=NewFile (Uploadurl +filename); if(!targetfile.exists ()) { Try{targetfile.createnewfile (); } Catch(IOException e) {e.printstacktrace (); } } //move an upload file Try{File.transferto (targetfile); } Catch(IOException e) {e.printstacktrace (); } System.out.println ("http://127.0.0.1:8080/study2/upload/" +filename); return NULL; }
}
Multiple file uploads:
@RequestMapping ("/moreupload") PublicString moreupload (httpservletrequest request) {multiparthttpservletrequest Multiparthttpservletrequest=(multiparthttpservletrequest) request; Map<string, multipartfile> files =Multiparthttpservletrequest.getfilemap (); String Uploadurl= Request.getsession (). Getservletcontext (). Getrealpath ("/") + "upload/"; //determines whether the corresponding path exists, does not exist, createsFile dir =NewFile (Uploadurl); if(!dir.exists ()) {Dir.mkdir (); } List<String> fileList =NewArraylist<string>(); for(Multipartfile file:files.values ()) {file TargetFile=NewFile (Uploadurl +file.getoriginalfilename ()); if(!targetfile.exists ()) { Try{targetfile.createnewfile (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Try{File.transferto (targetfile); Filelist.add ("http://localhost:8080/study2/upload/" +file.getoriginalfilename ()); } Catch(IllegalStateException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }}} Request.setattribute ("Files", fileList); System.out.println (Files.size ()); return NULL; }
File Download:
@Controller Public classDownloadcontroller {@RequestMapping ("/download") Publicstring DownLoad (string fileName, httpservletrequest request, httpservletresponse response) {Response.setconten Ttype ("Text/html;charset=utf-8"); Try{request.setcharacterencoding ("UTF-8"); } Catch(unsupportedencodingexception e) {e.printstacktrace (); } Bufferedinputstream bis=NULL; Bufferedoutputstream Bos=NULL; String Ctxpath= Request.getsession (). Getservletcontext (). Getrealpath ("/") + "upload/"; String Downloadpath= Ctxpath +FileName; System.out.println (Downloadpath); Try { LongFilelength =NewJava.io.File (downloadpath). Length (); Response.setcontenttype ("Application/x-msdownload;"); Response.setheader ("Content-disposition", "attachment; Filename= "+NewString (Filename.getbytes ("Utf-8"), "Iso8859-1")); Response.setheader ("Content-length", String.valueof (filelength)); Bis=NewBufferedinputstream (NewFileInputStream (Downloadpath)); Bos=NewBufferedoutputstream (Response.getoutputstream ()); byte[] Buff =New byte[2048]; intBytesread; while((bytesread = bis.read (buff, 0, buff.length))! =-1) {bos.write (buff,0, Bytesread); } }Catch(Exception e) {e.printstacktrace (); }finally { Try{bis.close (); } Catch(IOException e) {e.printstacktrace (); } Try{bos.close (); } Catch(IOException e) {e.printstacktrace (); } } return NULL; }}
java File upload and download