如何访问不再tomcat工程下的文件

其实我是真性情 2009-11-09 09:16:55
我的TOMCAT在C盘,我想把用户上传的文件保存在D盘,比如D盘跟目录下D:/。如果用户上传了一个a.doc。现在我想让用户下载自己刚才上传过的文件,但是a.doc不再我的工程下,我再JSP上怎么写呢?总不能写个绝对路径D:/a.doc吧,我想写相对路径,请问我需要这么做,请说的详细一些,谢谢
...全文
1187 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 lwb314 的回复:]
引用 4 楼 bao110908 的回复:
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/>吗?
[/Quote]

可以配置多个,但是虚拟路径尽量不要跟你的实际路径冲突了。
Samuely 2009-11-18
  • 打赏
  • 举报
回复
补充下:
如果是下载的话,只能通过Servlet读取,然后写到流就OK了
Samuely 2009-11-18
  • 打赏
  • 举报
回复
JSP不干这事,仅负责显示的部分。
客户端上传的文件,交给Servlet去处理,这个就没什么问题,直接绝对路径也能搞,关JSP什么事
windforcecn 2009-11-18
  • 打赏
  • 举报
回复
总得存下客户端上传路径的

持久化到数据库也是可以考虑的
villagehead 2009-11-18
  • 打赏
  • 举报
回复
我的解决办法是专门写一个读入流,
然后写给下载用的输出流。

因为服务器的程序读取服务器端的文件没什么限制,
只要路径写对了就能读到,
然后写给下载用的response的outputstream就行了。

这样的好处是可以在写出去的时候,加上一些判断,
比如这个用户(session中的)是否合法等
不过这么做感觉很费内存。

也期待更好的解决方案。

good luck
sforiz 2009-11-18
  • 打赏
  • 举报
回复
UP
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 bao110908 的回复:]
tomcat 的 server.xml 中配置虚拟路径。

假如你的工程名为 news,上传后的目录为 d:/upload

在 server.xml 中加上一行:

<Context path="/news/upload" docBase="d:/upload" />

以后就可以使用 http://localhost:8080/news/upload/20091111/xxx.doc 访问了。
[/Quote]
这里请问1下,如果我有很多东西,可以配置多个<Context/>吗?
  • 打赏
  • 举报
回复
可以配置多个

sangshusen_1988 2009-11-18
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 lwb314 的回复:]
引用 4 楼 bao110908 的回复:
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/>吗?
[/Quote]
可以配置多个,不过都可以再目录下在分目录,配置多个没啥必要
jackyren007 2009-11-11
  • 打赏
  • 举报
回复
Redirect()?
  • 打赏
  • 举报
回复
tomcat 的 server.xml 中配置虚拟路径。

假如你的工程名为 news,上传后的目录为 d:/upload

在 server.xml 中加上一行:

<Context path="/news/upload" docBase="d:/upload" />

以后就可以使用 http://localhost:8080/news/upload/20091111/xxx.doc 访问了。
  • 打赏
  • 举报
回复
没有其他办法吗?通过配置什么的
amdgaming 2009-11-09
  • 打赏
  • 举报
回复
mark
swandragon 2009-11-09
  • 打赏
  • 举报
回复
连接用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);
%>

今天做项目,用到了el表达式和jstl,在tomcat6.0上跑得好好的,没想到copy到tomcat5.5后,服务器起不来了,是tomcat版本的问题。查阅了不少资料,现在总结如下: 错误信息: java.lang.NoClassDefFoundError: javax/el/ExpressionFactory 分析:少包,上网查阅后发现解决方法 解决方法:将javaee.jar包再copy一份放在tomcat目录的common\lib下 如上完成后即解决了该异常,大以为大功告成,没想到又出现了新异常 错误信息:java.lang.NoSuchMethodError: javax.servlet.JSP.PageContext.getELContext()LJAVAx/el/ELContext 分析:这个问题网上找了半天没看到解决方法,只好自己想办法。想起先前看到的一个知识点:tomcat 6 实现了 servlet 2.5 和JSP2.1的规范相应的 tomcat 5 实现的是 2.4和2.0,想到tomcat5不支持JSTL1.2,于是替换JSTL1.1的包试试看后,问题解决 解决方法:移出工程lib目录中的JSTL1.2.jar,添加standard-1.1.jar和JSTL1.1.jar 总结: jstl1.1版本由2个必要包构成:standard-1.1.jar和JSTL1.1.jar;而jstl1.1版本只有一个必要包JSTL1.2.jar。(一开始在替换的过程中想当然的用JSTL1.1.jar换JSTL1.2.jar,然后遗漏了standard-1.1.jar,耽搁了不少时间。) 经测试,tomcat6.0支持JSTL1.2版本,也支持JSTL1.1版本;tomcat5.5只支持JSTL1.1(JSTL1.1一下版本未测试) 不建议将2个版本的3个包同时放在工程中,可能会发生版本冲突。(另外,实际操作的时候发现JSTL1.2.jar和JSTL1.1.jar这2个包放一起可以在tomcat5.5和tomcat6.0上跑,鉴于JSTL1.1.jar里面没有c.tld等文件,猜测JSTL1.1.jar里面只是放置了1.1的专用的类和信息)

62,616

社区成员

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

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