通过Servlet下载文件的路径问题

Saro 2004-04-30 09:05:18
package ebusiness.event;
/**
* <p>Title:文件下载类 </p>
* <p>Description:需要下载文件时使用此类 </p>
* <p>Copyright: Copyright (c) 2004</p>
* @author kewell
* @version 1.0
*/

import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.RequestDispatcher;
import java.util.ResourceBundle;

/**
*用于下载文件的servlet
*@vesion 1.0
*/
public class DownloadFile extends HttpServlet {
//字符编码
private final String ENCODING="GB2312";
//内容类型
private final String CONTENT_TYPE="text/html;charset=GB2312";
//要下载的文件存放的路径
private String downloadFileDir="download\\";

/**
*执行HTTP get操作
* @param req 从用户来的请求
* @param resp servlet的回应
*/
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
//设置request对象的字符编码
request.setCharacterEncoding(ENCODING);
//从request 中取出要下载文件的名字
String fileName=request.getParameter("filename");
if(fileName==null || fileName.trim().equals("")){
//设置response对象ContentType
response.setContentType(CONTENT_TYPE);
//输出错误信息
PrintWriter out=response.getWriter();
out.println("<font color=red>输入的文件名无效!</font>");
out.close();
}
else{
//下载文件的完整路径名
String fullFileName=downloadFileDir+fileName;
System.out.println("下载文件:"+fullFileName);
//根据文件的类型设置response对象ContentType
String contentType =getServletContext().getMimeType(fullFileName);
if(contentType==null)
contentType="application/octet-stream";
response.setContentType(contentType);
response.setHeader("content-disposition","attachment;filename=\""+fileName+"\"");
InputStream is=null;
OutputStream os=null;
try{
is=new BufferedInputStream(new FileInputStream(fullFileName));
//定义输出字节流
java.io.ByteArrayOutputStream baos=new ByteArrayOutputStream();
//定义response的输出流
os =new BufferedOutputStream(response.getOutputStream());
//定义buffer
byte[] buffer=new byte[4*1024];//4k Buffer
int read =0;
//从文件中读入数据并写到输出字节流中
while ((read=is.read(buffer))!=-1){
baos.write(buffer,0,read);
}
//将输出字节流写到response的输出流中
os.write(baos.toByteArray());
}
catch (IOException e){
e.printStackTrace();
}
finally{
//关闭输出字节流和response输出流
os.close();
is.close();
}
}

}
public void doPost(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException{
//调用doGet方法
doGet(req,resp);
}

}

程序没错,只是路径总是找不到,预下载的文件为webapp\business\download\xx.rar
如果有另外的可控制用户下载文件的方法,一样给分,在线等候
...全文
684 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuguangjie 2004-08-06
  • 打赏
  • 举报
回复
我使用了该类加上request.getRealPath()得到跟目录路径可以下载文件,但是每次下载文件默认都是html格式,我要求默认是所有文件格式怎么修改
wuguangjie 2004-08-06
  • 打赏
  • 举报
回复
使用request.getRealPath("")得到当前目录
Saro 2004-04-30
  • 打赏
  • 举报
回复
嗯,解决了,有点乱来的干法。
把下载的文件放在类文件同一个目录里,
然后 is=getClass().getResourceAsStream(fileName);
请教达人是否有另外控制用户下载文件的好办法?
narilee 2004-04-30
  • 打赏
  • 举报
回复
你可以用println语句将当前路径信息打印出来看看啊,呵呵
Saro 2004-04-30
  • 打赏
  • 举报
回复
不行呢,我的问题是不知道 “当前路径” 是那里!
yzh315 2004-04-30
  • 打赏
  • 举报
回复
download 前面加上 \\ 试试
private String downloadFileDir="\\download\\";
Saro 2004-04-30
  • 打赏
  • 举报
回复
<servlet-mapping>
<servlet-name>DownloadFile</servlet-name>
<url-pattern>/DownloadFile.jsp</url-pattern>
</servlet-mapping>

使用:
http://localhost:8080/business/DownloadFile.jsp?fileName=xx.rar
Saro 2004-04-30
  • 打赏
  • 举报
回复
business是我的网站根目录
treeClimber 2004-04-30
  • 打赏
  • 举报
回复
"business\\download"?

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧