无法更改Tomcat的默认文件上传目录

时间:2019-04-25 00:13:28

标签: java tomcat file-upload web-applications

我正在创建样本文件上传的网页。我正在指定自己的路径。但是,它始终将文件上传到我的工作区(Eclipse工作区)的.metadata文件夹中。这是代码:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
           boolean isMultipart = ServletFileUpload.isMultipartContent(request);
           if (isMultipart) {
               FileItemFactory factory = new DiskFileItemFactory();
               ServletFileUpload upload = new ServletFileUpload(factory);

                   List 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("/cloudnine/photobucket");
                           File path = new File(root);
                           if (!path.exists()) {
                               boolean status = path.mkdirs();
                           }
                           File uploadedFile = new File(path + "/" + fileName);
                           System.out.println(uploadedFile.getAbsolutePath());
                           item.write(uploadedFile);
                       }
                   }
               } 
           }

保存该文件的位置是: tanmayaworkspace / .metadata / .plugins / org.eclipse.wst.server.core / tmp0 / wtpwebapps / cloudnine / cloudnine / photobucket / ESA.pdf

但是我希望它是: tanmayaworkspace / cloudnine / photobucket / ESA.pdf

0 个答案:

没有答案