Spring MVC implementation File download

Source: Internet
Author: User

Download File ①

The download file requires a byte array to be restored as a file.

First, use MyBatis to isolate the byte array in the database, specifying the file name (including the format). Then use OutputStream to enter the file

  1. @RequestMapping (value = "Downphotobyid")
  2. public void Downphotobystudentid (String ID, Final httpservletresponse response) {
  3. photoentity entity = this.photoMapper.getPhotoEntityByPhotoId (ID);
  4. byte[] data = Entity.getphotodata ();
  5. String fileName = entity.getfilename () = = null? "Photo. png": Entity.getfilename ();
  6. filename = urlencoder.encode (filename, "UTF-8");
  7. Response.reset ();
  8. Response.setheader ("Content-disposition", "attachment; Filename=\ "" + fileName + "\" ");
  9. Response.AddHeader ("Content-length", "" "+ data.length);
  10. Response.setcontenttype ("Application/octet-stream;charset=utf-8");
  11. OutputStream outputstream = new Bufferedoutputstream (Response.getoutputstream ());
  12. Outputstream.write (data);
  13. Outputstream.flush ();
  14. Outputstream.close ();
  15. }

    1. <a href= "<%=request.getcontextpath ()%>/downphotobyid.do?id=8000001" > Download photos </a>

Download File ②

/** * @Description Download file

* @author JXLDJSN

* @date December 11, 2015 6:11:33

* @param fileName

* @param file

* @return

* @throws IOException

*/

Public responseentity<byte[]> Download (String fileName, File file) throws IOException {

String dfilename = new String (Filename.getbytes ("gb2312"), "iso8859-1");

Httpheaders headers = new Httpheaders (); Headers.setcontenttype (Mediatype.application_octet_stream); Headers.setcontentdispositionformdata ("Attachment", dfilename);

return new responseentity<byte[]> (Fileutils.readfiletobytearray (file), headers, httpstatus.created); }

Download File ③

Main methods of File download  PublicStaticvoid Download (HttpServletRequest request,  HttpServletResponse response, String storeName, String ContentType )Throws Exception {   Request.setcharacterencoding ("UTF-8"); Bufferedinputstream bis =Null Bufferedoutputstream BOS =Null  Get the project root directory String Ctxpath = Request.getsession (). Getservletcontext ()   . Getrealpath ("");    Get download File shoulder String Downloadpath = ctxpath+"/uploadfile/" + storeName;  Get the length of a file  Long filelength =New File (Downloadpath). Length ();  Set file Output Type Response.setcontenttype ("Application/octet-stream"); Response.setheader ("Content-disposition","Attachment; Filename= "   +New String (Storename.getbytes ("Utf-8"),"Iso8859-1"));  Setting the output length Response.setheader ("Content-length", String.valueof (Filelength));  Get input stream bis =New Bufferedinputstream (New FileInputStream (Downloadpath));  Output stream 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); 
                                                                                                         }   //Close stream  bis.close ();  bos.close ();  }  }
                                                                                                       < /c10> 


Download direct access controller such as: http:\\localhost:8080/springmvc/download.do


Download File ④

  1. @RequestMapping ("/export")
  2. Public responseentity<byte[]> Export () throws IOException {
  3. Httpheaders headers = new Httpheaders ();
  4. Headers.setcontenttype (Mediatype.application_octet_stream);
  5. Headers.setcontentdispositionformdata ("attachment", "Dict.txt");
  6. return new responseentity<byte[]> (Fileutils.readfiletobytearray ("New File" ("c:/users/ Administrator/desktop/a.txt ")),
  7. headers, httpstatus.created);
  8. }


Spring MVC implementation File download

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.