路径问题(咋就没人来回答呢)

youfeng445 2010-12-02 11:39:41

public ActionForward upload(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException {
System.out.println("当前路径:" + request.getContextPath());
CustomerForm cf = (CustomerForm) form;
FormFile formFile = cf.getExcelFile();
SessionContainer sc = SessionContainerUtils.getSessionContainer();
String upPath = "";
if (formFile != null) {
System.out.println();// 打印出实际上传时所选文件的文件名

String fileName = formFile.getFileName();

// 生成文件名为:当前登录帐号_原文件名_当前时间戳.原文件后缀
upPath = "/upload/" //************************* + sc.getLoginId()
+ "_"
+ fileName.substring(0, fileName.lastIndexOf("."))
+ "_"
+ System.currentTimeMillis()
+ fileName.substring(fileName.lastIndexOf("."), fileName
.length());
try {
FileOutputStream fos = new FileOutputStream(upPath);// 获取文件流对象
fos.write(formFile.getFileData());// 开始写入
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}



现在在webconent下有个upload,我想把上传的东西存到这个文件夹,请问//************************* 处的路径怎么写
/upload/不行...
...全文
225 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuyongai0563 2010-12-03
  • 打赏
  • 举报
回复
可以用虚拟目录来解决,上传文件放到web下面合适吗?
Hallelujah_chen 2010-12-03
  • 打赏
  • 举报
回复
lz 建议在写文件前先判断你所选路径的文件夹是否存在,或则把该文件夹的绝对路径debug出来,这样出什么错也容易判读了。
youfeng445 2010-12-03
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 muzipeng2008 的回复:]
this.getServletContext().getRealPath("/")+"upload/"
[/Quote]
空指针
muzipeng2008 2010-12-03
  • 打赏
  • 举报
回复
this.getServletContext().getRealPath("/")+"upload/"
youfeng445 2010-12-03
  • 打赏
  • 举报
回复
我的项目是smsserver..项目是在eclipse上开发的,所以webroot文件夹默认是webcontent
upload是在webcontent下的

这个是获取tomcat路径
private String getSavePath() {
PropertyResourceBundle res = (PropertyResourceBundle) ResourceBundle
.getBundle("Framework");
String tomcatPath = System.getProperty("catalina.home");
tomcatPath = tomcatPath.replace("/", File.separator) + "/webapps";
String savePath = tomcatPath + res.getString("upload");
savePath = savePath.replace("/", File.separator);
return savePath;
}
配置文件Framework.properties属性:

upload=/SMSServer/upload


这样获取的我用的tomcat路径..而用eclipse部署的tomcat是在他自己的Workspaces下...所以问一下怎么获取eclipse下tomcat的路径...
ethenjean 2010-12-03
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 youfeng445 的回复:]
引用 3 楼 zhongxingxuan 的回复:
Java code


request.getSession().getServletContext().getRealPath("/") + "upload/";

The method getServletContext() is undefined for the type HttpSession

没这个方法
[/Quote]
怎么可能会没有这个方法呢?
风中叶 2010-12-03
  • 打赏
  • 举报
回复
String path =StaticConst.getInstance().getRealRootPath()+"/upload";

我目前也在做文件上传 只不过指定上传的位置是项目跟目录下的upload文件夹下: 项目名称/upload/
dashuaishuiyun 2010-12-03
  • 打赏
  • 举报
回复
楼主,可能是tomcat的设置问题,前不久我也是生成文件,可是在localhost下面可以生成,可是一放到其他的服务器,也是报这个错:文件找不到。。。。
ousyuryu 2010-12-03
  • 打赏
  • 举报
回复
在path赋值语句下面加上
upPath = request.getServletContext().getRealPath(upPath);
仲兴轩 2010-12-03
  • 打赏
  • 举报
回复
HttpServletRequest request
youfeng445 2010-12-03
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zhongxingxuan 的回复:]
Java code


request.getSession().getServletContext().getRealPath("/") + "upload/";
[/Quote]
The method getServletContext() is undefined for the type HttpSession

没这个方法
仲兴轩 2010-12-03
  • 打赏
  • 举报
回复


request.getSession().getServletContext().getRealPath("/") + "upload/";

youfeng445 2010-12-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 focusforce 的回复:]
upload/,不是/upload/
[/Quote]
java.io.FileNotFoundException: upload\s_相关报表_1291305584375.xls (系统找不到指定的路径。)
focusforce 2010-12-02
  • 打赏
  • 举报
回复
upload/,不是/upload/

81,092

社区成员

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

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