关于jsp下载时乱码问题

<%@ page language="java" contentType="text/html; charset=gbk"
	pageEncoding="GB18030" import="java.sql.*,java.util.*,java.io.*"%>

<%@include file="config.jsp"%>

<%	
	String Save_Location=getServletContext().getRealPath("/")+"upload//";
 

    File userBox=new File((Save_Location)+"//");
    File userBoxfile[]=userBox.listFiles();
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>

		<title>下载</title>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<link rel="stylesheet" type="text/css" href="styles.css">
		<style type="text/css">
<!--
body 
{
	background-color: #D6D6D6;
	background-image: url(image/e.jpg);
}

.ww1 {
	height: 30px;
	width: 180px;
}
#apDiv1 {
	position: absolute;
	left: 657px;
	top: 70px;
	width: 408px;
	height: 247px;
	z-index: 1;
}
-->
</style>
<script> 
function openmydoc(path)

{ 
var doc=new ActiveXObject("Word.Application"); 
doc.visible=true; 
doc.Documents.Open(path); 
} 
</script> 
	</head>
<body>

	
	      
	<h3 align="right"> </a> </h3>  


		<br />
	<table width="1825" height="204" border="1" align="center">
		<tr>
			<td height="26" align="left">
				<font color="red">欢迎

				
				<br>
		    </td>
	    </tr>
		<tr>
			<td height="58" align="left" valign="top">
		文件列表
			
			</a>
				<% for(int i=0;i<userBoxfile.length;i++){%>
  <tr><td height="22">
  
  
					<p>
					 文件名称 &nbsp;&nbsp;&nbsp;  
								<font color="#FF00FF" size="7"><%=userBoxfile[i].getName()%></font></p></td><td height="25">
										<span style="font-size: 9pt">
										 
										
							
														<A style="background: #2BA82E; padding: 7px 10px; border-radius: 4px; border: 2px solid #2BA82E; border-image: none; color: rgb(255, 255, 255); font-weight: bold;" 

href="DownServlet?filename=<%=userBoxfile[i].getName()%>" target=_top> <font  size="-2" color="#FFFFFF">下载文档</font></A> 
											</a>
									
										
										
										</span></td>
										
										
										
										<td height="25"><span style="font-size: 9pt">
											
											
											
											
											
											
				
											
											
											</span>
										</td>
				
	  </tr>
					<%}%>
			</td>
		</tr>
    </table>


	</body>
</html>

DownServlet.java

package com.servlet;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
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 DownServlet extends HttpServlet {

	/**
	 * 文件的上传和下载
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {

		//获得请求文件名  
			request.setCharacterEncoding("ISO-8859-1");
		String filename = request.getParameter("filename");  
		System.out.println("=========================================="+filename);
		filename =new String(filename.getBytes("ISO-8859-1"),"GBK");
		System.out.println("=========================================="+filename);
			
		
		 System.out.println(filename);  

		 //设置文件MIME类型  
		 response.setContentType(getServletContext().getMimeType(filename));  
		 //设置Content-Disposition  
		 System.out.println("测试"+filename);
		 response.setHeader("Content-Disposition", "attachment;filename="+new String(filename.getBytes("GBK"),"ISO-8859-1"));  
		 //读取目标文件,通过response将目标文件写到客户端  
		 //获取目标文件的绝对路径 

		 
		 String fullFileName = getServletContext().getRealPath("/upload/" + filename);  
		 System.out.println("11"+fullFileName);  
		 //读取文件  
		 InputStream in = new FileInputStream(fullFileName);  
		 OutputStream out = response.getOutputStream();  

		 //写文件  
		 int b;  
		 while((b=in.read())!= -1)  
		 {  
			 out.write(b);  
		 }  

		 in.close();  
		 out.close();  
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		/*	request.setCharacterEncoding("ISO-8859-1");
		String filename = request.getParameter("filename");  
		System.out.println("=========================================="+filename);
		filename =new String(filename.getBytes("ISO-8859-1"),"GBK");
		System.out.println("=========================================="+filename);*/
		//获得请求文件名  
		String filename = request.getParameter("filename");  
		filename = new String(filename.getBytes("UTF-8"),"ISO8859-1");
		System.out.println(filename);  

		//设置文件MIME类型  

		response.setContentType("application/x-download");

		//设置Content-Disposition  
		System.out.println(filename);  
		response.setHeader("Content-Disposition", "attachment;filename="+filename);  
		//读取目标文件,通过response将目标文件写到客户端  
		//获取目标文件的绝对路径  
		String fullFileName = getServletContext().getRealPath("/upload/" + filename);  
		System.out.println("测试"+fullFileName);  
		//读取文件  
		request.setCharacterEncoding("ISO-8859-1");
		System.out.println("=========================================="+fullFileName);
		fullFileName =new String(fullFileName.getBytes("ISO-8859-1"),"GBK");
		System.out.println("=========================================="+fullFileName);

		InputStream in = new FileInputStream(fullFileName);  
		OutputStream out = response.getOutputStream();  

		//写文件  
		int b;  
		while((b=in.read())!= -1)  
		{  
			out.write(b);  
		}  

		in.close();  
		out.close();  
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值