登录成功后完成文件的下载(中文乱码)


<!DOCTYPE html>
<html>
<head>
<title>1.html</title>

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">

<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
	<h1>文件下载列表文件</h1>
	<a href="/J2EE/download/cat.jpg">cat.jpg</a>
	<h1>手动编码的方式下载</h1>
	<a href="/J2EE/downloadservlet?filename=cat.jpg">cat.jpg</a>
	<!-- 问号表示给服务器传的参数 -->
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>1.html</title>

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">

<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
	<h1>文件下载列表文件</h1>
	<a href="/J2EE/download/cat.jpg">cat.jpg</a>
	<h1>手动编码的方式下载</h1>
	<a href="/J2EE/downloadservlet?filename=cat.jpg">cat.jpg</a>
	<!-- 问号表示给服务器传的参数 -->
</body>
</html>
package javapack;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class downloadservlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//1.接收参数
		String filename = new String(request.getParameter("filename").getBytes("ISO-8859-1"),"UTF-8");//通过html传来的参数获得filename,并且解决中文乱码问题
		System.out.println(filename);
		//2.完成文件下载
		//2.1设置Content-Type
		String type = this.getServletContext().getMimeType(filename);//获得文件类型
		response.setHeader("Content-Type", type);
		//2.3设置文件的InputStream
		InputStream is = this.getServletContext().getResourceAsStream("/download/"+filename);
		//浏览器下载中文名文件,文件名乱码解决
		String agent=request.getHeader("User-Agent");//获得浏览器版本
		System.out.println(agent);
		if (agent.contains("FireFox")){
			//比较复杂,不要求
		}
		else{
			filename=URLEncoder.encode(filename,"UTF-8");
		}
		//2.2设置Content-Disposition头
		response.setHeader("Content-Disposition","attachment;filename="+filename);//防止直接用浏览器打开,文件下载名为filename

		//获得response的输出流,把项目中的文件传给浏览器
		ServletOutputStream sos = response.getOutputStream();
		int len=0;
		byte[]b=new byte[1024];
		while((len=is.read(b))!=-1){
			sos.write(b,0,len);
		}
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet( reque





补充,火狐浏览器解决中文乱码

package javapack;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class output extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//test1(response);
		/*
		 * 使用字符流输出中文
		 * 一定会乱码,response使用的字符流有缓冲区,编码是ISO-8859,不支持中文
		 */
		response.setHeader("Content-Type", "text/html;charset=UTF-8");//设置浏览器默认打开的字符编码
		response.setCharacterEncoding("UTF-8");//设置response的字符流缓冲区编码
		//response.setContentType("UTF-8");//此一句话顶上面两句
		response.getWriter().print("你好");;//把write方法用字符串输出
	}

	private void test1(HttpServletResponse response) throws IOException {
		/*
		 * 通过字节流向浏览器输出
		 */
		response.getOutputStream().write("你好".getBytes("UTF-8"));//若getBytes()方法与浏览器输出显示编码一致,则不会乱码
		response.setHeader("Content-Type", "text/html;charset=UTF-8");//设置浏览器打开格式
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,  response);

	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值