The first step is to support selecting the front desk for uploading files:[HTML] View plain copy <%@ 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 only needs to be annotated @requstmapping ("Your request name") before the request.[Java] View plain copy package hope.cs.zhku.controller; import java.io.file; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.multipart.multipartfile; import org.springframework.web.multipart.multiparthttpservletrequest; /*********************** * Name: Userbasiceditorcontroller.java </br> * Date:2011-8-15</br> * features:</br> * Written by: Willson huang</br> * Review:</br> * other:</br> * history: (description, modifier, Time) </br> * 1.create ,Willson Huang ,2011-8-15 **************** / @Controller public class uploadcontroller { @RequestMapping ("Upload.do") public string upload (Httpservletrequest request, httpservletresponse response ) { Multiparthttpservletrequest multiparthttpservletrequest= (multiparthttpservletrequest) request; MultipartFile multipartFile = Multiparthttpservletrequest.getfile ("UploadFile"); String originalfilename=mUltipartfile.getoriginalfilename (); file file= New file ("File"); if (!file.exists ()) { file.mkdir (); } System.out.println (File.getabsolutepath ()); try { fileoutputstream fileoutputstream=new fileoutputstream (file+ "/uploadfile" +originalfilename.substring ( Originalfilename.lastindexof ('. '), originalfilename.length ()); fileoutputstream.write (Multipartfile.getbytes ()); fileoutputstream.flush (); fileoutputstream.close (); } catch (filenotfoundexception e) { // TODO Auto-generated catch block e.printstacktrace (); return "Error"; } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace (); return "Error"; } return " Uploadsuccess "; } }
Second, the file download
Implementation is front desk:[HTML] View plain copy <%@ 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 ">
The front desk fills in the file to download, the background looks up 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)[Java] View plain copy package hope.cs.zhku.controller; import java.io.file; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import javax.servlet.http.httpservletresponse; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; /****************************** * Name: userbasiceditorcontroller.java</br > * Date:2011-8-15</br> * function:</br> * written: Willson Huang</br> * Review:</br> * other:</br> * history :(description, modified by person, time) </br> * 1.create ,Willson Huang ,2011-8-15 ************************************ / @Controller public class downloadcontroller { @RequestMapping ("download.do") public void downloadfile (string filename,httpservletresponse response) { response.setcharacterencoding (" Utf-8 "); response.setcontenttype (" multipart/ Form-data "); response.setheader (" Content-disposition ", " Attachment;filename= "+filename); try { file file=new file (Filename); system.out.println ( File.getabsolutepath ()); Inputstream inputstream=new fileinputstream ("file/" +file); outputstream os=response.getoutputstream (); byte[] b=new byte[1024]; int length; while ((Length=inputStream.read (b)) > 0) { os.write (b,0,length); } &NBSP;&NBsp; inputstream.close (); } catch (filenotfoundexception e) { e.printstacktrace (); } catch (ioexception e) { e.printstacktrace (); } } }