Front Desk:<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding= "UTF-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >call the upload.do request in the background spring MVC simply annotations @requstmapping before the request ("Your request Name") can correspond to the background: PackageHope.cs.zhku.controller;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.multipart.MultipartFile;Importorg.springframework.web.multipart.MultipartHttpServletRequest;/****************************************************************************** * Name: UserBasicEditorController.java </br> * Date:2011-8-15</br> * Function:</br> * Written: Willson huang</br> * Review:</br> * other:</br> * Calendar History: (Description, modified by person, time) </br> * 1.create, Willson Huang, 2011-8-15 ****************************************************** ***********************/@Controller Public classUploadcontroller {@RequestMapping ("Upload.do") PublicString Upload (httpservletrequest request,httpservletresponse response) {Multiparthttpservletrequest multipart HttpServletRequest=(multiparthttpservletrequest) request; Multipartfile Multipartfile= Multiparthttpservletrequest.getfile ("UploadFile"); String OriginalFilename=Multipartfile.getoriginalfilename (); File File=NewFile ("File"); if(!file.exists ()) {File.mkdir (); } System.out.println (File.getabsolutepath ()); Try{FileOutputStream FileOutputStream=NewFileOutputStream (file+ "/uploadfile" +originalfilename.substring (Originalfilename.lastindexof ('. ')), Originalfilename.length ()); Fileoutputstream.write (Multipartfile.getbytes ()); Fileoutputstream.flush (); Fileoutputstream.close (); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); return"Error"; } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); return"Error"; } return"Uploadsuccess"; At this point, the configuration file, as shown in the previous article configuration, is no longer listed here, the controller has an output statement, output file storage location, you can change the copy code download: Copy Code front desk:<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding= "UTF-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >$ (function () {$ (A). Click (function () {var link= This. href+ '? ' + ' filename= ' +$ (' #fileName ')). Val (); window.open (link); return false; }); });</script>backstage: The front desk fills in the file to be downloaded, the backstage searches from the folder, if does not have the file to return the error file, otherwise provides the download of any file type (must write the suffix when filling in the file) PackageHope.cs.zhku.controller;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping;/****************************************************************************** * Name: UserBasicEditorController.java </br> * Date:2011-8-15</br> * Function:</br> * Written: Willson huang</br> * Review:</br> * other:</br> * Calendar History: (Description, modified by person, time) </br> * 1.create, Willson Huang, 2011-8-15 ****************************************************** ***********************/@Controller Public classDownloadcontroller {@RequestMapping ("Download.do") Public voiddownloadFile (String filename,httpservletresponse response) {response.setcharacterencoding ("Utf-8"); Response.setcontenttype ("Multipart/form-data"); Response.setheader ("Content-disposition", "attachment;filename=" +fileName); Try{File File=NewFile (fileName); System.out.println (File.getabsolutepath ()); InputStream InputStream=NewFileInputStream ("file/" +file); OutputStream OS=Response.getoutputstream (); byte[] b=New byte[1024]; intlength; while((Length=inputstream.read (b)) >0) {Os.write (b,0, length); } inputstream.close (); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }}} Copy code
Springmvc File Upload Download