如何访问不再tomcat工程下的文件 我的TOMCAT在C盘,我想把用户上传的文件保存在D盘,比如D盘跟目录下D:/。如果用户上传了一个a.doc。现在我想让用户下载自己刚才上传过的文件,但是a.doc不再我的工程下,我再JSP上怎么写呢?总不能写个绝对路径D:/a.doc吧,我想写相对路径,请问我需要这么做,请说的详细一些,谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 连接用a.doc下载页面重组下载文件地址d:/a.doc文件保存地址,存放在配置文件中<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <tr bgcolor="#FFFFFF" align="center"> <td align="left" colspan="2" width="" height="20px" nowrap="nowrap"> <img src="images/mail_affix.gif" alt="下载" style="cursor: hand"> <font color=navy>out.xml<a href="download.jsp?filePath=out.xls">下载</a> </td> </tr> </body></html>down.jsp<%@ page contentType="text/html;charset=GBK" pageEncoding="UTF-8"%><%@ page import="java.io.*,java.util.*,java.text.*"%><%! public static boolean checkFor304(HttpServletRequest req,File file){ if("no-cache".equalsIgnoreCase(req.getHeader("Pragma")) || "no-cache".equalsIgnoreCase(req.getHeader("cache-control"))){ }else{ String thisTag = Long.toString(file.lastModified()); String eTag = req.getHeader("If-None-Match"); if(eTag != null){ if(eTag.equals(thisTag)){ return true; } } DateFormat rfcDateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z"); Date lastModified = new Date(file.lastModified()); try{ long ifModifiedSince = req.getDateHeader("If-Modified-Since"); if(ifModifiedSince != -1){ long lastModifiedTime = lastModified.getTime(); if(lastModifiedTime <= ifModifiedSince){ return true; } }else{ try{ String s = req.getHeader("If-Modified-Since"); if(s != null){ Date ifModifiedSinceDate = rfcDateFormat.parse(s); if(lastModified.before(ifModifiedSinceDate)){ return true; } } }catch(ParseException e){ e.printStackTrace(); } } }catch(IllegalArgumentException e){ e.printStackTrace(); } } return false; }%><% //从配置文件中读取path String realPath = "d:/"; //文件下载地址 String filePath = realPath + request.getParameter("filePath"); if(filePath != null){ filePath = new String(filePath.getBytes("iso-8859-1"),"gb2312"); } boolean isInline = false; out.clear(); response.reset(); try{ java.io.File f = new java.io.File(filePath); if(f.exists() && f.canRead()){ if(checkFor304(request,f)){ response.sendError(HttpServletResponse.SC_NOT_MODIFIED); return; } String mimetype = null; mimetype = application.getMimeType(filePath); if(mimetype == null){ mimetype = "application/x-download;charset=ISO8859-1"; } response.setContentType(mimetype); String ua = request.getHeader("User-Agent"); // if(ua == null) ua = "User-Agent: Mozilla/4.0 (compatible;MSIE 6.0;)"; boolean isIE = ua.toLowerCase().indexOf("msie") != -1; // if(isIE && !isInline){ mimetype = "application/x-msdownload"; } //System.out.println("mimetype====================="+mimetype+"\n"); String downFileName = new String(f.getName().getBytes(),"ISO8859-1"); String inlineType = isInline ? "inline" : "attachment"; // response.setHeader("Content-Disposition",inlineType + ";filename=\"" + downFileName + "\""); response.setContentLength((int) f.length()); byte[] buffer = new byte[4096]; BufferedOutputStream output = null; BufferedInputStream input = null; try{ output = new BufferedOutputStream(response.getOutputStream()); input = new BufferedInputStream(new FileInputStream(f)); int n = (-1); while((n = input.read(buffer,0,4096)) > -1){ output.write(buffer,0,n); } response.flushBuffer(); }catch(Exception e){ e.printStackTrace(); }finally{ if(input != null) input.close(); if(output != null) output.close(); } }else{%> <script> alert("file not found or file not exists !"); </script><% } return; }catch(Exception ex){ ex.printStackTrace(); } response.sendError(404);%> tomcat 的 server.xml 中配置虚拟路径。假如你的工程名为 news,上传后的目录为 d:/upload在 server.xml 中加上一行:<Context path="/news/upload" docBase="d:/upload" />以后就可以使用 http://localhost:8080/news/upload/20091111/xxx.doc 访问了。 这里请问1下,如果我有很多东西,可以配置多个<Context/>吗? 我的解决办法是专门写一个读入流,然后写给下载用的输出流。因为服务器的程序读取服务器端的文件没什么限制,只要路径写对了就能读到,然后写给下载用的response的outputstream就行了。这样的好处是可以在写出去的时候,加上一些判断,比如这个用户(session中的)是否合法等不过这么做感觉很费内存。也期待更好的解决方案。good luck JSP不干这事,仅负责显示的部分。客户端上传的文件,交给Servlet去处理,这个就没什么问题,直接绝对路径也能搞,关JSP什么事 补充下:如果是下载的话,只能通过Servlet读取,然后写到流就OK了 myeclipse控制台乱码问题 对于文本的按时间统计行数 开源两个小程序。 JComboBox异常 reverse()函的使用, 为什么不能自动编译关联的源程序了?[散分] 有关equals的程序 有关生成脚本语言并加入到网页上的问题 问个小方法。 java 值的传递 JFileChooser 保存文件时文件名为*?出错 如何通过接口利用反射获取另外一个类的属性。急,在线等!
下载页面重组下载文件地址
d:/a.doc
文件保存地址,存放在配置文件中
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<tr bgcolor="#FFFFFF" align="center">
<td align="left" colspan="2" width="" height="20px" nowrap="nowrap">
<img src="images/mail_affix.gif" alt="下载" style="cursor: hand">
<font color=navy>out.xml<a href="download.jsp?filePath=out.xls">下载</a>
</td>
</tr>
</body>
</html>
down.jsp<%@ page contentType="text/html;charset=GBK" pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*,java.text.*"%>
<%! public static boolean checkFor304(HttpServletRequest req,File file){
if("no-cache".equalsIgnoreCase(req.getHeader("Pragma"))
|| "no-cache".equalsIgnoreCase(req.getHeader("cache-control"))){
}else{
String thisTag = Long.toString(file.lastModified());
String eTag = req.getHeader("If-None-Match");
if(eTag != null){
if(eTag.equals(thisTag)){
return true;
}
}
DateFormat rfcDateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
Date lastModified = new Date(file.lastModified());
try{
long ifModifiedSince = req.getDateHeader("If-Modified-Since"); if(ifModifiedSince != -1){
long lastModifiedTime = lastModified.getTime();
if(lastModifiedTime <= ifModifiedSince){
return true;
}
}else{
try{
String s = req.getHeader("If-Modified-Since");
if(s != null){
Date ifModifiedSinceDate = rfcDateFormat.parse(s);
if(lastModified.before(ifModifiedSinceDate)){
return true;
}
}
}catch(ParseException e){
e.printStackTrace();
}
}
}catch(IllegalArgumentException e){
e.printStackTrace();
}
} return false;
}%>
<%
//从配置文件中读取path
String realPath = "d:/";
//文件下载地址
String filePath = realPath + request.getParameter("filePath");
if(filePath != null){
filePath = new String(filePath.getBytes("iso-8859-1"),"gb2312");
}
boolean isInline = false;
out.clear();
response.reset();
try{
java.io.File f = new java.io.File(filePath);
if(f.exists() && f.canRead()){
if(checkFor304(request,f)){
response.sendError(HttpServletResponse.SC_NOT_MODIFIED);
return;
}
String mimetype = null;
mimetype = application.getMimeType(filePath);
if(mimetype == null){
mimetype = "application/x-download;charset=ISO8859-1";
}
response.setContentType(mimetype);
String ua = request.getHeader("User-Agent"); //
if(ua == null) ua = "User-Agent: Mozilla/4.0 (compatible;MSIE 6.0;)";
boolean isIE = ua.toLowerCase().indexOf("msie") != -1; //
if(isIE && !isInline){
mimetype = "application/x-msdownload";
}
//System.out.println("mimetype====================="+mimetype+"\n"); String downFileName = new String(f.getName().getBytes(),"ISO8859-1");
String inlineType = isInline ? "inline" : "attachment"; //
response.setHeader("Content-Disposition",inlineType + ";filename=\"" + downFileName + "\"");
response.setContentLength((int) f.length());
byte[] buffer = new byte[4096];
BufferedOutputStream output = null;
BufferedInputStream input = null;
try{
output = new BufferedOutputStream(response.getOutputStream());
input = new BufferedInputStream(new FileInputStream(f));
int n = (-1);
while((n = input.read(buffer,0,4096)) > -1){
output.write(buffer,0,n);
}
response.flushBuffer();
}catch(Exception e){
e.printStackTrace();
}finally{
if(input != null) input.close();
if(output != null) output.close();
}
}else{
%>
<script>
alert("file not found or file not exists !");
</script>
<%
}
return;
}catch(Exception ex){
ex.printStackTrace();
}
response.sendError(404);
%>
然后写给下载用的输出流。因为服务器的程序读取服务器端的文件没什么限制,
只要路径写对了就能读到,
然后写给下载用的response的outputstream就行了。这样的好处是可以在写出去的时候,加上一些判断,
比如这个用户(session中的)是否合法等
不过这么做感觉很费内存。也期待更好的解决方案。good luck
客户端上传的文件,交给Servlet去处理,这个就没什么问题,直接绝对路径也能搞,关JSP什么事
如果是下载的话,只能通过Servlet读取,然后写到流就OK了