Download files with Chinese names

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>下载页面</title>
</head>
<body>
<a href="/day15/img/九尾.jpg">图片1</a>
<a href="/day15/img/1.avi">视屏</a>
<hr/>
<a href="/day15/downloadServlet?filename=九尾.jpg">图片1</a>
<a href="/day15/downloadServlet?filename=1.avi">Screen </ HTML></ body></a>




package com.hopetesting.web.utils;

import sun.misc.BASE64Encoder;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;


public class DownLoadUtils {

public static String getFileName(String agent, String filename) throws UnsupportedEncodingException {
if (agent.contains("MSIE")) {
// IE浏览器
filename = URLEncoder.encode(filename, "utf-8");
filename = filename.replace("+", " ");
} else if (agent.contains("Firefox")) {
// 火狐浏览器
BASE64Encoder base64Encoder = new BASE64Encoder();
filename = "=?utf-8?B?" + base64Encoder.encode(filename.getBytes("utf-8")) + "?=";
} else {
// 其它浏览器
filename = URLEncoder.encode(filename, "utf-8");
}
return filename;
}
}



package com.hopetesting.web.download;

import com.hopetesting.web.utils.DownLoadUtils;
import sun.management.Agent;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;

/**
* @author newcityman
* @Date 2019/9/2 - 0:05
* /
@WebServlet ( "/ downloadServlet")
public class DownloadServlet the extends the HttpServlet {
protected void the doPost (the HttpServletRequest Request , the HttpServletResponse Response) throws ServletException , IOException {
//. 1, the parameter acquisition request , the file name
String filename = request.getParameter ( "filename") ;
// 2, using the file input stream of bytes loaded into memory
// 2.1, the file path to find
the ServletContext context = request.getServletContext () ;
String realpath = context.getRealPath ( "/ IMG /" + filename) ;
// 2.2, the byte stream associated with
FIS = the FileInputStream new new the FileInputStream (realpath);
// 3, is provided in response to the first response
// 3.1-type information Content
String mimeType = context.getMimeType (filename) ;
response.setHeader ( "Content-type" , mimeType) ;
// 3.2 setting response content-disposition header
// Chinese document processing garbled
// Get user-agent request-header
String = request.getHeader Agent ( "user-agent") ;
// encoding method using the utility class name of the file
filename = DownLoadUtils. getFileName (Agent , filename ) ;
response.setHeader ( "Content-Disposition" , "Attachment; filename =" + filename) ;
//. 4, the input data stream is written to the output stream
the ServletOutputStream response.getOutputStream SOS = () ;
int len =0;
byte[] buff = new byte[1024*8];
while( (len=fis.read(buff))!=-1){
sos.write(buff,0,len);
}
fis.close();
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
 

Guess you like

Origin www.cnblogs.com/newcityboy/p/11444417.html