Upload
1.jar Package with Springmvc.xml
Package the request into multiparthttpservletrequest.
2. Page
Note that method is post
3.action
Method one parsing (request)
Method two (parameter)
Download
- @RequestMapping ("/download/{filename}")
- Public modelandview Download (@PathVariable ("FileName")
- String FileName, httpservletrequest request, httpservletresponse response)
- throws Exception {
- Response.setcontenttype ("Text/html;charset=utf-8");
- Request.setcharacterencoding ("UTF-8");
- Java.io.BufferedInputStream bis = null;
- Java.io.BufferedOutputStream BOS = null;
- String Ctxpath = Request.getsession (). Getservletcontext (). Getrealpath (
- "/")
- + " \ \" + "images\\";
- String Downloadpath = Ctxpath + fileName;
- System.out.println (Downloadpath);
- try {
- Long filelength = new File (Downloadpath). Length ();
- Response.setcontenttype ("application/x-msdownload;");
- Response.setheader ("content-disposition", "attachment; Filename= "
- + New String (Filename.getbytes ("Utf-8"), "iso8859-1"));
- Response.setheader ("Content-length", String.valueof (Filelength));
- bis = new Bufferedinputstream (new FileInputStream (Downloadpath));
- BOS = New Bufferedoutputstream (Response.getoutputstream ());
- byte[] Buff = new byte[2048];
- int bytesread;
- While (-1! = (Bytesread = bis.read (buff, 0, Buff.length))) {
- Bos.write (Buff, 0, Bytesread);
- }
- } catch (Exception e) {
- E.printstacktrace ();
- } finally {
- if (bis! = null)
- Bis.close ();
- if (bos! = null)
- Bos.close ();
- }
- return null;
- }
- }
Springmvc File Upload and download