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
- @RequestMapping (value = "Downphotobyid")
- public void Downphotobystudentid (String ID, Final httpservletresponse response) {
- photoentity entity = this.photoMapper.getPhotoEntityByPhotoId (ID);
- byte[] data = Entity.getphotodata ();
- String fileName = entity.getfilename () = = null? "Photo. png": Entity.getfilename ();
- filename = urlencoder.encode (filename, "UTF-8");
- Response.reset ();
- Response.setheader ("Content-disposition", "attachment; Filename=\ "" + fileName + "\" ");
- Response.AddHeader ("Content-length", "" "+ data.length);
- Response.setcontenttype ("Application/octet-stream;charset=utf-8");
- OutputStream outputstream = new Bufferedoutputstream (Response.getoutputstream ());
- Outputstream.write (data);
- Outputstream.flush ();
- Outputstream.close ();
- }
- <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 ④
- @RequestMapping ("/export")
- Public responseentity<byte[]> Export () throws IOException {
- Httpheaders headers = new Httpheaders ();
- Headers.setcontenttype (Mediatype.application_octet_stream);
- Headers.setcontentdispositionformdata ("attachment", "Dict.txt");
- return new responseentity<byte[]> (Fileutils.readfiletobytearray ("New File" ("c:/users/ Administrator/desktop/a.txt ")),
- headers, httpstatus.created);
- }
Spring MVC implementation File download