使用java servlet访问的HTML 5文件上传拒绝异常

时间:2014-12-02 15:08:32

标签: java html5 file tomcat servlets

我正在C:/ xampp / tomcat / temp文件夹中尝试文件上传功能,因为文件出现在应有的位置。但是,响应会返回以下错误:

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)

如果有的话,我该怎么办?

2 个答案:

答案 0 :(得分:1)

我认为您的问题是在写入文件系统时未指定文件名 你应该做点什么:

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);

我希望这有帮助。

答案 1 :(得分:0)

试试这个我希望它会对你有所帮助。如果你有不同的用例说出来。我会试试

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));
            }
}