JavaWeb- file download (to solve the garbage problem)

download file

  1. Direct use of a label come and go download

Some of the content will automatically parse the browser, the browser will not parse the file is downloaded, do not use this method.

  1. Servlet come and download by sending a request
    by sending a Servlet request to send the file name to the server, after sending to the server, receiving the file name parameter to get the absolute address of the file, in the form of streams come and written to the browser.
    I had to tell what type of file, the browser is to identify the type of MIME types.
this.getServletContext().getMimeType(“文件名称”);

Set response header type (MIME)

response.setContentType("MIME类型")
//即
response.setContentType(this.getServletContext().getMimeType(“文件名称”));

Setting response headers, to tell the browser not to resolve, in the form of an attachment is opened

response.setHeader("Content-Dsiposition","attachment;filename="+文件名)

Step
1. Receive a file name argument
2. Get the mime type
3. Set the browser response type
4. browser to download an attachment
5. Obtain the file absolute path
6. Read the file stream
7. obtain an output stream
8. contents written to the output stream

File Path:
Here Insert Picture Description
JSP:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<a href="/Servlet6?filename=1.jpg">1.jpg</a><br/>
<a href="/Servlet6?filename=a.txt">a.txt下载</a>
</body>
</html>

@WebServlet("/Servlet6")
public class Servlet6 extends HttpServlet {
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收文件名参数
        String filename = request.getParameter("filename");
        /* 对接收的参数进行编码处理
        * 获取参数,默认会对参数进行编码ISO8859-1
        * 把乱码转成二进制位*/
        byte[] bytes = filename.getBytes("ISO8859-1");
        /*使用UTF-8进行编码*/
        filename = new String(bytes,"UTF-8");
        /*根据文件名来获取mime类型*/
        String mime = this.getServletContext().getMimeType(filename);
        /*设置mimeType*/
        response.setContentType(mime);
        /*告诉浏览器中文编码*/
        // 获取客户端信息
        String agent = request.getHeader("User-Agent");
        // 定义一个变量记录编码之后的名字
        String filenameEncoder = "";
        if (agent.contains("MSIE")) {
            // IE编码
            filenameEncoder = URLEncoder.encode(filename, "utf-8");
            filenameEncoder = filenameEncoder.replace("+", " ");
        } else if (agent.contains("Firefox")) {
            // 火狐编码
            BASE64Encoder base64Encoder = new BASE64Encoder();
            filenameEncoder = "=?utf-8?B?" + base64Encoder.encode(filename.getBytes("utf-8")) + "?=";
        } else {
            // 浏览器编码
            filenameEncoder = URLEncoder.encode(filename, "utf-8");
        }
        response.setHeader("Content-Disposition", "attachment;filename="+filenameEncoder);
        //获取文件的绝对路径
        String path = this.getServletContext().getRealPath("img/"+filename);
        System.out.println(path);
        //3.读取文件流
        FileInputStream in = new FileInputStream(path);
        //4.获取输出流
        ServletOutputStream out = response.getOutputStream();
        //5.把内容写出到输出流
        byte[] buffer = new byte[1024];
        int len = 0;
        while((len = in.read(buffer)) != -1) {
            out.write(buffer, 0, len);
        }
        in.close();
    }


}
Published 25 original articles · won praise 0 · Views 278

Guess you like

Origin blog.csdn.net/qq_42219004/article/details/105300050