Let’s first look at the simplest example of file download:
Copy the code code as follows:
package com.yyz.response;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//File download
public class ResponseDemo extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String realpath = this.getServletContext().getRealPath("/download/1.gif");
String filename = realpath.substring(realpath.lastIndexOf("//")+1);
response.setHeader("content-disposition", "attachment;filename="+filename);
//The server uses this header to tell the browser to open the data in download mode
FileInputStream in = new FileInputStream(realpath);
int len = 0;
byte buffer[]=new byte[1024];
OutputStream out = response.getOutputStream();
while((len = in.read(buffer))>0){
out.write(buffer, 0, len);
}
in.close();
//There is no need to close out. When the response is destroyed, the server will automatically close the stream related to the response.
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
The function of this code is to download picture 1.png from the server. The directory structure is displayed as follows using MyEclipse's package explorer:
Let's make it a little more difficult. The file we want to download is a file with a Chinese name. Since the things in the header file in the http protocol can only be ASCII characters, the above method (directly change String realpath = this.getServletContext().getRealPath("/download/1.gif"); to String realpath = this .getServletContext().getRealPath("/download/picture.gif");) Get the file directly,
There will be garbled code problems. Attached are the test results:
To solve this problem, use the encode method of the URLEncoder class:
Copy the code code as follows:
package com.yyz.response;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//When downloading Chinese files, the Chinese file names must be URL encoded.
public class ResponseDemo extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String realpath = this.getServletContext().getRealPath("/download/picture.gif");
String filename = realpath.substring(realpath.lastIndexOf("//")+1);
response.setHeader("content-disposition", "attachment;filename="+URLEncoder.encode(filename,"UTF-8"));
//This function encodes the string into URL
FileInputStream in = new FileInputStream(realpath);
int len = 0;
byte buffer[]=new byte[1024];
OutputStream out = response.getOutputStream();
while((len = in.read(buffer))>0){
out.write(buffer, 0, len);
}
in.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
Attached are the test results:
In addition, there is a small detail that everyone needs to pay attention to:
FileReader cannot be used instead of FileInputStream. Data will be lost when using FileReader. The reason is this: FileReader is a character stream, and data such as pictures and media files are stored in the form of 01. When reading with FileReader, you need to consult an encoding table. If an encoding is not specified, The default encoding for the corresponding platform is used. For example, computers in China will check GB2312. When a code that does not exist in the GB2312 code table is read, the data will be encoded into '? ', the data will become Chinese and '? 'Mix. After sending it to the client, check the code table again when displaying it, and replace all '?' with '?' codes, and the data will be lost. Just remember one thing about this detail: byte streams can handle any type of data, character streams can only handle character data.