下载处理乱码
Posted 缺氧的鱼520
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了下载处理乱码相关的知识,希望对你有一定的参考价值。
Servlet类:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1、获取被下载的资源的名称
String name = request.getParameter("name");
name = new String(name.getBytes("ISO-8859-1"),"UTF-8");
// 告诉浏览器进行保存处理
//设置下载的文件的类型
ServletContext sc = this.getServletContext();
String mt = sc.getMimeType("/ziliao/" + name);
response.setHeader("content-type", mt);
//获取浏览器信息
String agent = request.getHeader("user-agent");
String downloadName = DownLoadUtils.getName(agent, name);
System.out.println(downloadName);
response.setHeader("Content-Disposition", "attachment; filename = " + downloadName);
//2、获取资源对应的输入流
InputStream in = sc.getResourceAsStream("/ziliao/" + name);
//3、读这个流,并响应到浏览器
ServletOutputStream out = response.getOutputStream();
byte[] buffer = new byte[1024];
int length;
while((length = in.read(buffer)) != -1){
out.write(buffer, 0, length);
}
//4、关流
in.close();
}
//版本3:让浏览器进行保存处理
/*protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1、获取被下载的资源的名称
String name = request.getParameter("name");
name = new String(name.getBytes("ISO-8859-1"),"UTF-8");
// 告诉浏览器进行保存处理
* Content-Type:mime类型
* Content-Disposition: attachment; filename =
*
//设置下载的文件的类型
ServletContext sc = this.getServletContext();
String mt = sc.getMimeType("/ziliao/" + name);
response.setHeader("content-type", mt);
response.setHeader("Content-Disposition", "attachment; filename = " + name);
//2、获取资源对应的输入流
InputStream in = sc.getResourceAsStream("/ziliao/" + name);
//3、读这个流,并响应到浏览器
ServletOutputStream out = response.getOutputStream();
byte[] buffer = new byte[1024];
int length;
while((length = in.read(buffer)) != -1){
out.write(buffer, 0, length);
}
//4、关流
in.close();
}*/
//版本2:数据读写
/*protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1、获取被下载的资源的名称
String name = request.getParameter("name");
name = new String(name.getBytes("ISO-8859-1"),"UTF-8");
//2、获取资源对应的输入流
ServletContext sc = this.getServletContext();
InputStream in = sc.getResourceAsStream("/ziliao/" + name);
//3、读这个流,并响应到浏览器
ServletOutputStream out = response.getOutputStream();
byte[] buffer = new byte[1024];
int length;
while((length = in.read(buffer)) != -1){
out.write(buffer, 0, length);
}
//4、关流
in.close();
}*/
//版本1:框架搭建
/*protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1、获取被下载的资源的名称
String name = request.getParameter("name");
name = new String(name.getBytes("ISO-8859-1"),"UTF-8");
System.out.println(name);
//2、获取资源对应的输入流
//3、读这个流,并响应到浏览器
}*/
要有一个下载工具类:DownLoadUtils:
package cn.itcast.web.util;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import sun.misc.BASE64Encoder;
public class DownLoadUtils {
public static String getName(String agent, String filename) throws UnsupportedEncodingException {
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;
}
}
要有2个jar包:
以上是关于下载处理乱码的主要内容,如果未能解决你的问题,请参考以下文章