Problem Description:
The file download function is a frequently used feature in web development, and the HttpServletResponse object can be used to download files. But the download task is working, and the downloaded image is empty.
Problem code:
//download a picture from the server//Create a byte read stream to read the picture locally//Get Picture PathServletContext context = This. Getservletcontext (); String Path= Context.getrealpath ("web-inf/classes/jpg"); FileInputStream FIS=NewFileInputStream (path); //Create a byte output stream send a picture to the clientServletoutputstream SOS =Response.getoutputstream (); //process the download file nameString fileName = path.substring (path.lastindexof ("\ \") +1); //handling file Names Chinese garbled problemString name = Urlencoder.encode (FileName, "Utf-8"); //notifies the browser to open the file in the way it is downloadedResponse.setheader ("Content-disposition", "attachment;filename=" +name); Response.setheader ("Content-type", "Image/jpeg"); //creating an array of characters byte[] arr =New byte[1024]; intLen = 0; if(len = Fis.read (arr))! =-1) {sos.write (arr),0, Len); } fis.close (); Sos.close ();
Problem Analysis:
First, the picture successfully saved to the local, it is possible to set up the header through the response to download is feasible, then, the picture content is empty can only be read and write when the problem is carefully checked, found in the Read and write section will be written if, resulting in, write the file only in the first valid, and then cannot write
Problem solving:
Change if to while
//download a picture from the server//Create a byte read stream to read the picture locally//Get Picture PathServletContext context = This. Getservletcontext (); String Path= Context.getrealpath ("web-inf/classes/jpg"); FileInputStream FIS=NewFileInputStream (path); //Create a byte output stream send a picture to the clientServletoutputstream SOS =Response.getoutputstream (); //process the download file nameString fileName = path.substring (path.lastindexof ("\ \") +1); //handling file Names Chinese garbled problemString name = Urlencoder.encode (FileName, "Utf-8"); //notifies the browser to open the file in the way it is downloadedResponse.setheader ("Content-disposition", "attachment;filename=" +name); Response.setheader ("Content-type", "Image/jpeg"); //creating an array of characters byte[] arr =New byte[1024]; intLen = 0; while(len = Fis.read (arr))! =-1) {sos.write (arr),0, Len); } fis.close (); Sos.close ();
Summary of issues:
// process the download file name String fileName = path.substring (path.lastindexof ("\ \") +1) ; // handling file Names Chinese garbled problem String name = Urlencoder.encode (FileName, "Utf-8");
// notifies the browser to open the file in the way it is downloaded Response.setheader ("Content-disposition", "attachment;filename=" +name); Response.setheader ("Content-type", "image/jpeg");
Path problem for file under SRC
The root directory of the WEB project refers to the beginning of webroot/web-inf/classes. Java's class file can only read files in this directory. If you want to read files elsewhere, you can use an absolute path.
If you are reading a file through Java class, such as "De la section". jpg, it can only be placed in the Webroot/web-inf/classes directory. That is, in the project SRC, will be automatically compiled into the webroot/web-inf/classes directory
Reference Link: http://blog.csdn.net/zhangqindabendan/article/details/53002544
Web Download Picture is empty