能帮忙改下吗?我试了很多方法 都不成功 怎么弄 下载时候的文件名都是乱码高手帮帮忙吧package test;import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;public class DownloadServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK"; //Initialize global variables
public void init() throws ServletException {
} //Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE); String path="E:\\测试.doc";
String fileName="测试.doc";
File file = new File(path);
response.setCharacterEncoding("GBK");
response.setContentType("application/x-msdownload");
response.setContentLength( (int) file.length());
response.setHeader("Content-Disposition", "attachment;filename="+fileName);
FileInputStream fis = new FileInputStream(file);
BufferedInputStream fbis = new BufferedInputStream(fis);
byte abyte0[] = new byte[1024];
int k = 0;
OutputStream out = response.getOutputStream();
while ( (long) k < file.length())
{
int j = fbis.read(abyte0, 0, 1024);
k += j;
out.write(abyte0, 0, j);
}
out.flush(); } //Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
} public static String FormatString(String str)
{
if (str==null)
{
str="";
}
try
{
byte b[]=str.getBytes("ISO-8859-1");
str=new String(b);
return str;
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("字符转换错误!");
return null;
}
}
//Clean up resources
public void destroy() {
}
}
import javax.servlet.http.*;
import java.io.*;
import java.util.*;public class DownloadServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK"; //Initialize global variables
public void init() throws ServletException {
} //Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE); String path="E:\\测试.doc";
String fileName="测试.doc";
File file = new File(path);
response.setCharacterEncoding("GBK");
response.setContentType("application/x-msdownload");
response.setContentLength( (int) file.length());
response.setHeader("Content-Disposition", "attachment;filename="+fileName);
FileInputStream fis = new FileInputStream(file);
BufferedInputStream fbis = new BufferedInputStream(fis);
byte abyte0[] = new byte[1024];
int k = 0;
OutputStream out = response.getOutputStream();
while ( (long) k < file.length())
{
int j = fbis.read(abyte0, 0, 1024);
k += j;
out.write(abyte0, 0, j);
}
out.flush(); } //Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
} public static String FormatString(String str)
{
if (str==null)
{
str="";
}
try
{
byte b[]=str.getBytes("ISO-8859-1");
str=new String(b);
return str;
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("字符转换错误!");
return null;
}
}
//Clean up resources
public void destroy() {
}
}
FileInputStream fis = new FileInputStream(fileUrl);
OutputStream os = response.getOutputStream();
String fileName = fileUrl.substring(fileUrl.lastIndexOf("/")+1);
String fileNameEncoder = URLEncoder.encode(fileName, "utf-8"); //设置编码
response.setHeader("Content-disposition", "attachment; filename="
+ fileNameEncoder);
int temp = 0;
while ((temp = fis.read()) != -1) {
os.write(temp);
}
String path="E:\\测试.doc";
String fileName="测试.doc";
String disFileName = new String(fileName.getBytes("GBK"),"ISO8859_1");
File file = new File(path);
response.setCharacterEncoding("GBK");
response.setContentType("application/x-msdownload");
response.setContentLength( (int) file.length());
response.setHeader("Content-Disposition", "attachment;filename="+disFileName);
.......
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException { //word要这么写的
response.setContentType("application/msword;charset=gb2312"); String path="E:\\测试.doc";
String fileName="测试.doc"; //显示中文文件名
response.setHeader("Content-disposition","attachment; filename="
+new String( fileName.getBytes("GBK"), "ISO8859_1" )); FileInputStream fis = new FileInputStream(file);
BufferedInputStream fbis = new BufferedInputStream(fis);
byte abyte0[] = new byte[1024];
int k = 0;
......
还是别的类型的文件也要加相应的 东西