简体   繁体   English

使用Java Servlet访问的HTML 5文件上传被拒绝异常

[英]HTML 5 file upload using java servlet access denied exception

I'm trying the file upload feature, which seems to work, as the file appears where it should, in the C:/xampp/tomcat/temp folder. 我正在尝试文件上传功能,该功能似乎可以正常工作,因为文件在C:/ xampp / tomcat / temp文件夹中应显示的位置。 However, the response comes back as the following error: 但是,响应作为以下错误返回:

java.io.FileNotFoundException: C:\xampp\tomcat\temp (Access is denied) java.io.FileNotFoundException: C:\xampp\tomcat\temp (Access is denied)
The server encountered an internal error that prevented it from fulfilling this request.
java.io.IOException: java.io.FileNotFoundException: C:\xampp\tomcat\temp (Access is denied)


Main.FileUpload.doPost(FileUpload.java:50)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
root causejava.io.FileNotFoundException: C:\xampp\tomcat\temp (Access is denied)
    java.io.FileOutputStream.open(Native Method)
    java.io.FileOutputStream.<init>(Unknown Source)
    java.io.FileOutputStream.<init>(Unknown Source)
    org.apache.tomcat.util.http.fileupload.disk.DiskFileItem.write(DiskFileItem.java:391)
    Main.FileUpload.doPost(FileUpload.java:50)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

What should I do about this, if anything? 如果有的话,我该怎么办?

I think your problem is that not specified the file name when writing to the file system. 我认为您的问题是写入文件系统时未指定文件名。
You should do something like: 您应该执行以下操作:

String uploadPath  = "C:\xampp\tomcat\temp";
FileItem item;
....

String fileName = new File(item.getName()).getName();
String filePath = uploadPath + File.separator + fileName;
File storeFile = new File(filePath);
item.write(storeFile);

I hope this help. 希望对您有所帮助。

Try This I hope it will help you. 试试这个,希望对您有所帮助。 If you have different use case say it. 如果您有不同的用例,请说出来。 I will try 我会尝试

 if (ServletFileUpload.isMultipartContent(request)) { try { List<FileItem> multipart = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request); for (FileItem item : multipart) { if (!item.isFormField()) { filename = new File(item.getName()).getName(); folder = getServletContext().getRealPath("/") + "subfolders/"; File file = new File(folder); if (!file.exists()) { file.mkdirs(); } item.write(new File(folder + "/" + filename)); } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM