我有以下代码上传我的附件。但我无法将其上传到我自己的路径中。我该如何更改以下代码?

时间:2013-08-06 04:55:58

标签: upload path ejb-3.0 servlet-3.0 fileitem

      if (isMultipart) {
        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        try {
            // Parse the request
            List /* FileItem */ items = upload.parseRequest(request);
            Iterator iterator = items.iterator();
            while (iterator.hasNext()) {
                FileItem item = (FileItem) iterator.next();
               if (!item.isFormField()) {
                    String fileName = item.getName();
                    String root = getServletContext().getRealPath("/");
                    File path = new File(root+"/uploads");
                    if (!path.exists()) {
                        boolean status = path.mkdirs();
                        System.out.println("status"+status);
                    }
                    File uploadedFile = new File(path + "/" + fileName);
                    item.write(uploadedFile);                                         
                }
            }
        } catch (FileUploadException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

我的附件存储在与机器相关的位置。我如何将其更改为我自己的位置? (例如:-war /上传/ ..)。现在路径也显示我的机器名称。(例如: - / home / name / NetBeansProjects / prjname / dist / wldeploy / prjname / prjname-war.war / uploads / image.jpg)

0 个答案:

没有答案