commons fileUpload:在 Web 应用程序上下文中指定上传目录

发布于 2024-08-22 12:29:07 字数 4956 浏览 8 评论 0原文

我想知道如何指定上传目录,最好是我要存储上传文件的 WEB-CONTENT 目录下的目录的相对路径:当我指定上传存储为:

**File saveFile = new File("/"+fileName);** please refer to code below

错误:

INFO: Server startup in 497 ms
java.io.IOException: The system cannot find the path specified
 at java.io.WinNTFileSystem.createFileExclusively(Native Method)
 at java.io.File.createNewFile(Unknown Source)
 at controller.UploadServlet.processUploadedFile(UploadServlet.java:86)
 at controller.UploadServlet.doPost(UploadServlet.java:61)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
 at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
 at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
 at java.lang.Thread.run(Unknown Source)
java.io.IOException: Access is denied
 at java.io.WinNTFileSystem.createFileExclusively(Native Method)
 at java.io.File.createNewFile(Unknown Source)
 at controller.UploadServlet.processUploadedFile(UploadServlet.java:86)
 at controller.UploadServlet.doPost(UploadServlet.java:61)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
 at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
 at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
 at java.lang.Thread.run(Unknown Source)

我的代码:

**
 * Instantiates SempediaHome Controller
 */
public class UploadServlet extends HttpServlet {

 /**
  * 
  * @param
  * @return
  */
 public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {

  // Create a factory for disk-based file items
  DiskFileItemFactory factory = new DiskFileItemFactory();

  // Set factory constraints
  //factory.setSizeThreshold(yourMaxMemorySize);
  factory.setRepository(new File("/tmp"));

  // Create a new file upload handler
  ServletFileUpload upload = new ServletFileUpload(factory);

  // Set overall request size constraint
  //upload.setSizeMax(yourMaxRequestSize);

  // Parse the request
  try {
   List /* FileItem */ items = upload.parseRequest(request);
   Iterator iter = items.iterator();
   while (iter.hasNext()) {
       FileItem item = (FileItem) iter.next();

       if (item.isFormField()) {
           this.processFormField(item);
       } else {
           this.processUploadedFile(item);
       }
   }
  } catch (FileUploadException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

 /**
  * 
  * @param
  * @return
  */
 public void processUploadedFile(FileItem item) throws IOException {

  // Process a file upload
  if (!item.isFormField()) {
      //String fieldName = item.getFieldName();
      String fileName = item.getName();
      //String contentType = item.getContentType();
      //boolean isInMemory = item.isInMemory();
      //long sizeInBytes = item.getSize();     
      try {
       File saveFile = new File("/"+fileName);
       saveFile.createNewFile();
    item.write(saveFile);
   } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }

 /**
  * 
  * @param
  * @return
  */
 public void processFormField(FileItem item) {

 }

}

I'd like to know how to specify an upload directory preferably a relative path to a directory under my WEB-CONTENT directory where I'd like to store uploaded files: I get an error when I specify the upload be store as:

**File saveFile = new File("/"+fileName);** please refer to code below

Error:

INFO: Server startup in 497 ms
java.io.IOException: The system cannot find the path specified
 at java.io.WinNTFileSystem.createFileExclusively(Native Method)
 at java.io.File.createNewFile(Unknown Source)
 at controller.UploadServlet.processUploadedFile(UploadServlet.java:86)
 at controller.UploadServlet.doPost(UploadServlet.java:61)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
 at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
 at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
 at java.lang.Thread.run(Unknown Source)
java.io.IOException: Access is denied
 at java.io.WinNTFileSystem.createFileExclusively(Native Method)
 at java.io.File.createNewFile(Unknown Source)
 at controller.UploadServlet.processUploadedFile(UploadServlet.java:86)
 at controller.UploadServlet.doPost(UploadServlet.java:61)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
 at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
 at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
 at java.lang.Thread.run(Unknown Source)

my code:

**
 * Instantiates SempediaHome Controller
 */
public class UploadServlet extends HttpServlet {

 /**
  * 
  * @param
  * @return
  */
 public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {

  // Create a factory for disk-based file items
  DiskFileItemFactory factory = new DiskFileItemFactory();

  // Set factory constraints
  //factory.setSizeThreshold(yourMaxMemorySize);
  factory.setRepository(new File("/tmp"));

  // Create a new file upload handler
  ServletFileUpload upload = new ServletFileUpload(factory);

  // Set overall request size constraint
  //upload.setSizeMax(yourMaxRequestSize);

  // Parse the request
  try {
   List /* FileItem */ items = upload.parseRequest(request);
   Iterator iter = items.iterator();
   while (iter.hasNext()) {
       FileItem item = (FileItem) iter.next();

       if (item.isFormField()) {
           this.processFormField(item);
       } else {
           this.processUploadedFile(item);
       }
   }
  } catch (FileUploadException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

 /**
  * 
  * @param
  * @return
  */
 public void processUploadedFile(FileItem item) throws IOException {

  // Process a file upload
  if (!item.isFormField()) {
      //String fieldName = item.getFieldName();
      String fileName = item.getName();
      //String contentType = item.getContentType();
      //boolean isInMemory = item.isInMemory();
      //long sizeInBytes = item.getSize();     
      try {
       File saveFile = new File("/"+fileName);
       saveFile.createNewFile();
    item.write(saveFile);
   } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }

 /**
  * 
  * @param
  * @return
  */
 public void processFormField(FileItem item) {

 }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

筱果果 2024-08-29 12:29:07

在您的 servlet(或 jsp)中:

String contextRoot = getServletContext().getRealPath("/")

返回上下文根。所以:(

factory.setRepository(new File(contextRoot + "WEB-INF/tmp"));

你最好不要将tmp目录放在可以通过网络访问的地方)

你的异常意味着你当前的操作系统用户没有写入目标目录的权限。确保它具有所需目录的写权限。

In your servlet (or jsp):

String contextRoot = getServletContext().getRealPath("/")

returns the context root. So:

factory.setRepository(new File(contextRoot + "WEB-INF/tmp"));

(you'd better not put the tmp directory in a place accessible from the web)

Your exception means that your current OS user doesn't have permissions to write to the target directory. Make sure it was write permissions for the desired directory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文