Follow through progressive download files maven Response 03

1. Output message to the browser

    1.ServletOutputStream getOutputStream() throws IOException;

    2.PrintWriter getWriter() throws IOException;

2. Download the file

   1. To get the download file path

   2. Download the file name is valid

   3. Set find a way to download the browser can support what we needed

   4. Get to download the file input stream

   5. Create Buffer

   6. Get the OutputStream object

   7. The buffer is written to the stream buffer FileOutputStream

   8. Use OutputStream output data buffer to the client

Code:

@Override 
protected void doGet (REQ the HttpServletRequest, HttpServletResponse RESP) throws ServletException, IOException {
// 1. To get the download path of the file
String realPath = "E: \\ independent study \\ hao_web01 \\ response \\ src \\ main \ \ resouces \\ HuHao .png ";
System.out.println (" the downloaded files: "+ realpath);
// 2. what is the name of the downloaded file
String fileName = realPath.substring (realPath.lastIndexOf (" \ \ ") + 1);
// 3. set trying to make the browser to support (Content-Disposition) download what we need, Chinese file name URLEncoder.encode coding, or it may garbled
resp.setHeader (" Content-Disposition " , "Attachment; fileName =" + the URLEncoder.encode (fileName, "UTF-. 8"));
// 4. file input stream for downloading
the FileInputStream = new new in the FileInputStream (realpath);
//. 5.Create a buffer
int len = 0;
byte [] buffer = new new byte [1024];
// 6. The OutputStream object obtaining
the ServletOutputStream resp.getOutputStream OUT = ();
// 7. The buffer is written to the stream buffer FileOutputStream
while ((len = in.read (buffer ))> 0) {
out.write (buffer, 0, len);
}
// 8. the use OutputStream output data buffer to the client
in.close ();
the out.close ();
}

 

Guess you like

Origin www.cnblogs.com/huhao2000/p/11939777.html