java做文件下载
我做了一个java文件下载不能下中文
下面我把代码给大家看看帮我解决一下
我觉得问题有2个
1.用迅雷测试下载的时候下载所显示的文件名找不到只显示我的servlet名称必须自己写名称菜行
2.就是乱码工程报出异常就是文件名后面是乱码的异常就是找不到文件
String name=new String(request.getParameter("name").getBytes("iso-8859-1"),"utf-8");
//得到文件的路径也是tomcat服务器上面的路径文件上传都上传到了tomcat下面
String path=request.getSession().getServletContext().getRealPath("/upload/");
//要下载的文件名称
File file=new File(path+"/"+name);
//System.out.println(file.getName());
//设置下载文件的类型为任意类型
response.setContentType("application/x-msdownload");
//添加下载文件的头信息。此信息在下载时会在下载面板上显示,比如:
//迅雷下载显示的文件名称,就是此处filiname
response.addHeader("Content-Disposition","attachment;filename="+file.getName());
//添加文件的大小信息
response.setContentLength((int) file.length());
//获得输出网络流
ServletOutputStream sos=response.getOutputStream();
FileInputStream fis=new FileInputStream(file);
byte[] buffer=new byte[1024];
int i=0;
while((i=fis.read(buffer))!=-1){
sos.write(buffer,0,i);
sos.flush();
}
sos.close();
fis.close();
}
希望大虾们帮我解决下
[解决办法]
下载用smartupload多好 还用如此麻烦么
[解决办法]
我觉得把编码统一成UTF-8..
[解决办法]
用smartupload吧,多简单!
代码:
在第一个页面:
<a href="do_download.jsp">点击下载</a>
第二个页面,处理下载页面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@page import="com.jspsmart.upload.SmartUpload"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>My JSP 'do_download.jsp' starting page</title> </head> <body> <% SmartUpload smartUpload = new SmartUpload(); smartUpload.initialize(pageContext); smartUpload.setContentDisposition(null); //不让其自动显示 smartUpload.downloadFile("upload/1.gif"); %> </body></html>
[解决办法]
把编码先统一了,输出的时候,对流再进行一次编码。
[解决办法]
response.addHeader("Content-Disposition","attachment;filename="+file.getName());
我记得这里的文件名要用Urlencoding
[解决办法]
输出流用UTF-8进行编码即可
[解决办法]
String name=new String(request.getParameter("name").getBytes(),“ISO8859-1”);
[解决办法]
response.addHeader("Content-Disposition","attachment;filename="+URLEncoder.encode(file.getName(),"UTF-8"));
[解决办法]
直接把我做过的例子贴出来给你吧!
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import javax.servlet.RequestDispatcher;
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 FileDownServlet extends HttpServlet {
private static final String CONTENT_TYPE = "application/x-download; charset=UTF-8";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest req, HttpServletResponse res) throws
ServletException, IOException {
// 服务器相对路径
String path = req.getParameter("path").replace("/", "\\");
// 服务器绝对路径
path = getServletContext().getRealPath("/") + path;
// 检查文件是否存在
File obj = new File(path);
if (!obj.exists()) {
res.setContentType("text/html;charset=UTF-8");
res.getWriter().print("指定文件不存在!");
return;
}
// 读取文件名:用于设置客户端保存时指定默认文件名
int index = path.lastIndexOf("\\"); // 前提:传入的path字符串以“\”表示目录分隔符
String fileName = path.substring(index + 1);
// 写流文件到前端浏览器
ServletOutputStream out = res.getOutputStream();
res.setContentType("application/x-download");//设置为下载application/x-download
//res.setHeader("Content-disposition", "attachment;filename=" + fileName);
res.addHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode(fileName,"UTF-8"));
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(path));
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (IOException e) {
throw e;
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}
[解决办法]
response.addHeader("Content-Disposition","attachment;filename="+new String(file.getName().getBytes("iso8859_1"), "UTF-8");
[解决办法]