DownloadServlet:
package com.yuming.servlet;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
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;
import com.yuming.util.Base64Util;
/**
* Servlet implementation class DownloadServlet
*/
public class DownloadServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1. 获取要下载的文件名字 aa.jpg --- inputStream
String fileName = request.getParameter("filename");
//get请求,这个fileName有中文
fileName = new String(fileName.getBytes("UTF-8"),"UTF-8");
System.out.println("fileName="+fileName);
//2. 获取这个文件在tomcat里面的绝对路径地址
String path = getServletContext().getRealPath("download/"+fileName);
/**
* 如果文件的名字带有中文,需要对文件名进行编码处理
*
* IE/chrome 使用URLEncoding编码
*
* Firefox ,使用base64 编码
*/
String clientType = request.getHeader("User-Agent");
if(clientType.contains("Firefox")){
fileName = Base64Util.base64EncodeFileName(fileName);
}else{
//IE ,或者 Chrome (谷歌浏览器) ,
//对中文的名字进行编码处理
fileName = URLEncoder.encode(fileName, "