jsp网页下载文件出现中文乱码,我的下载超链接指向文件,如果文件名是英文,就下载正常,如果文件名是
jsp网页下载文件出现中文乱码,我的下载超链接指向文件,如果文件名是英文,就下载正常,如果文件名是中文,网页就会跳转进下载路径,报404,并且路径中的中文已被转为乱码。而...
jsp网页下载文件出现中文乱码,我的下载超链接指向文件,如果文件名是英文,就下载正常,如果文件名是中文,网页就会跳转进下载路径,报404,并且路径中的中文已被转为乱码。而且txt,dll等文件无论有没有中文名称也会进行跳转,不会正确弹出下载窗口,只能右键另存为。求解,调了几天了,项目追着要交。
展开
2016-12-10
展开全部
package Servlet;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.net.URLEncoder;
/**
* Servlet implementation class ServletDownload
*/
//@WebServlet(asyncSupported = true, urlPatterns = { "/ServletDownload" })
public class Download extends HttpServlet {
// private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Download() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("application/x-download");
request.setCharacterEncoding("utf-8");
//获得请求文件名
String filename = request.getParameter("filename");
System.out.println(filename);
//读取目标文件,通过response将目标文件写到客户端
//获取目标文件的绝对路径
String fullFileName = getServletContext().getRealPath("/download/" + filename);
String filencode = encodeFileName(request,filename);
//设置Content-Disposition
response.setHeader("Content-Disposition", "attachment;filename="+filencode);
//System.out.println(fullFileName);
File fileload = new File(fullFileName);
//通知客户文件的长度
long fileLength = fileload.length();
String length = String.valueOf(fileLength);
response.setHeader("Content_length", length);
try{
//读取文件
FileInputStream in = new FileInputStream(fileload);
OutputStream out = response.getOutputStream();
//写文件
byte b[] = new byte[600];
int n = 0;
while((n=in.read(b))!= -1)
{
out.write(b,0,n);
}
if(in != null)
{
in.close();
}
//
// out.close();
}catch(Exception exception){
System.out.println("Wrong");
exception.printStackTrace();
}
}
private String encodeFileName(HttpServletRequest request, String filename) {
// TODO Auto-generated method stub
try{
if(request.getHeader("User-Agent").toUpperCase().indexOf("MISE") > 0){
filename = URLEncoder.encode(filename,"UTF-8");
}
else {
filename = new String(filename.getBytes("UTF-8"),"IOS8859-1");
}
}catch(Exception exception){
exception.printStackTrace();
}
return filename;
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.net.URLEncoder;
/**
* Servlet implementation class ServletDownload
*/
//@WebServlet(asyncSupported = true, urlPatterns = { "/ServletDownload" })
public class Download extends HttpServlet {
// private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Download() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("application/x-download");
request.setCharacterEncoding("utf-8");
//获得请求文件名
String filename = request.getParameter("filename");
System.out.println(filename);
//读取目标文件,通过response将目标文件写到客户端
//获取目标文件的绝对路径
String fullFileName = getServletContext().getRealPath("/download/" + filename);
String filencode = encodeFileName(request,filename);
//设置Content-Disposition
response.setHeader("Content-Disposition", "attachment;filename="+filencode);
//System.out.println(fullFileName);
File fileload = new File(fullFileName);
//通知客户文件的长度
long fileLength = fileload.length();
String length = String.valueOf(fileLength);
response.setHeader("Content_length", length);
try{
//读取文件
FileInputStream in = new FileInputStream(fileload);
OutputStream out = response.getOutputStream();
//写文件
byte b[] = new byte[600];
int n = 0;
while((n=in.read(b))!= -1)
{
out.write(b,0,n);
}
if(in != null)
{
in.close();
}
//
// out.close();
}catch(Exception exception){
System.out.println("Wrong");
exception.printStackTrace();
}
}
private String encodeFileName(HttpServletRequest request, String filename) {
// TODO Auto-generated method stub
try{
if(request.getHeader("User-Agent").toUpperCase().indexOf("MISE") > 0){
filename = URLEncoder.encode(filename,"UTF-8");
}
else {
filename = new String(filename.getBytes("UTF-8"),"IOS8859-1");
}
}catch(Exception exception){
exception.printStackTrace();
}
return filename;
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询