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/不行...

解决方案 »

  1.   

    java.io.FileNotFoundException: upload\s_相关报表_1291305584375.xls (系统找不到指定的路径。)
      

  2.   

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

  3.   

    The method getServletContext() is undefined for the type HttpSession没这个方法
      

  4.   

    在path赋值语句下面加上
    upPath = request.getServletContext().getRealPath(upPath);
      

  5.   

    楼主,可能是tomcat的设置问题,前不久我也是生成文件,可是在localhost下面可以生成,可是一放到其他的服务器,也是报这个错:文件找不到。
      

  6.   

    String path =StaticConst.getInstance().getRealRootPath()+"/upload";我目前也在做文件上传 只不过指定上传的位置是项目跟目录下的upload文件夹下: 项目名称/upload/
      

  7.   

    我的项目是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的路径...
      

  8.   

    this.getServletContext().getRealPath("/")+"upload/"
      

  9.   

    lz 建议在写文件前先判断你所选路径的文件夹是否存在,或则把该文件夹的绝对路径debug出来,这样出什么错也容易判读了。
      

  10.   

    可以用虚拟目录来解决,上传文件放到web下面合适吗?