Web Download Picture is empty

Source: Internet
Author: User

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

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.