如何将文件上传到eclipse工作区

时间:2015-06-17 16:50:29

标签: java eclipse spring

我正在编写一个spring应用程序,它具有上传文件的功能,我知道如何将文件上传到h盘,但不知道如何在当前工作区上执行此操作。

这是我的servlet控制文件上传:

@RequestMapping(value="UploadServlet", method=RequestMethod.POST)
public void uploadfile(HttpServletRequest request, HttpServletResponse response) throws IOException{
    filePath = "/resources/images/";
     System.out.println(filePath);
    isMultipart = ServletFileUpload.isMultipartContent(request);
      response.setContentType("text/html");
      java.io.PrintWriter out = response.getWriter( );
      if( !isMultipart ){
         out.println("<html>");
         out.println("<head>");
         out.println("<title>Servlet upload</title>");  
         out.println("</head>");
         out.println("<body>");
         out.println("<p>No file uploaded</p>"); 
         out.println("</body>");
         out.println("</html>");
         return;
      }
      DiskFileItemFactory factory = new DiskFileItemFactory();
      // maximum size that will be stored in memory
      factory.setSizeThreshold(maxMemSize);
      // Location to save data that is larger than maxMemSize.
      factory.setRepository(new File("c:\\temp"));

      // Create a new file upload handler
      ServletFileUpload upload = new ServletFileUpload(factory);
      // maximum file size to be uploaded.
      upload.setSizeMax( maxFileSize );

      try{ 
      // Parse the request to get file items.
      List fileItems = upload.parseRequest(request);

      // Process the uploaded file items
      Iterator i = fileItems.iterator();

      out.println("<html>");
      out.println("<head>");
      out.println("<title>Servlet upload</title>");  
      out.println("</head>");
      out.println("<body>");
      while ( i.hasNext () ) 
      {
         FileItem fi = (FileItem)i.next();
         if ( !fi.isFormField () )  
         {
            // Get the uploaded file parameters
            String fileName = fi.getName();

            // Write the file
            if( fileName.lastIndexOf("\\") >= 0 ){
               file = new File( filePath + 
               fileName.substring( fileName.lastIndexOf("\\"))) ;
            }else{
               file = new File( filePath + 
               fileName.substring(fileName.lastIndexOf("\\")+1)) ;
            }
            System.out.println("Printing that thing");
            System.out.println(filePath + 
               fileName.substring(fileName.lastIndexOf("\\")+1));
            System.out.println(fileName.substring(fileName.lastIndexOf("\\")+1));

            fi.write( file ) ;

         }
      }

   }catch(Exception ex) {
       System.out.println(ex);
   }
    }
}

我知道问题出在文件路径变量

filePath = "/resources/images/";

我希望将文件写入Deployed Resources - &gt; webapp - &gt;资源 - &gt;图像

1 个答案:

答案 0 :(得分:0)

您可能希望使用ServletContext.html#getRealPath(java.lang.String)将相对资源转换为真实的文件系统路径。

String filePath = request.getServletContext().getRealPath("/resources/images/");

请注意,在一天结束时,这与Eclipse及其管理服务器和部署的方式关系不大。实际上,Eclipse可能会妨碍您在独立Web服务器中不会​​遇到的问题。例如,当Eclipse检测到项目中的更改时,它会重新部署文件,并可能会覆盖您更改或上传的任何内容。