java.io.IOException:java.io.FileNotFoundException:G:\ uploads(拒绝访问) com.efashion.controller.ProductController.doPost(ProductController.java:52) javax.servlet.http.HttpServlet.service(HttpServlet.java:644) javax.servlet.http.HttpServlet.service(HttpServlet.java:725) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
for (Part part : request.getParts()) {
String fileName = extractFileName(part);
String fieldName = part.getName();
if(fieldName.equalsIgnoreCase("file2")||fieldName.equalsIgnoreCase("file1")){
part.write(Utils.UPLOAD_DIRECTORY + File.separator + fileName);
}
//part.write(fileName);
}
request.getRequestDispatcher("viewproduct.jsp").forward(request, response);
}
private String extractFileName(Part part) {
String contentDisp = part.getHeader("content-disposition");
String[] items = contentDisp.split(";");
for (String s : items) {
if (s.trim().startsWith("filename")) {
System.out.println(s.substring(s.indexOf("=") + 2, s.length()-1));
return s.substring(s.indexOf("=") + 2, s.length()-1);
}
}
return "";
}
答案 0 :(得分:-1)
在我的情况下,当我发送多个文件上传但缺少某些文件时,它将引发FileNotFoundException(访问被拒绝)。因此,我向所有上传字段添加了必需的属性,并且可以正常工作。文件已成功上传。