查了类似错误,都是没有判断:是否创建路径(如下代码),添加了
if(!tempDirectory .exists()){
tempDirectory .mkdirs();
}
最终如下,但还是报错。
File tempDirectory = new File("d:\\tempDirectory");
if(!tempDirectory .exists()){
tempDirectory .mkdirs();
}
factory.setRepository(tempDirectory);
错误信息如下:
HTTP Status 500 – Internal Server Error
Type Exception Report
Message d:\files\D:\AAA.zip (文件名、目录名或卷标语法不正确。)
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
java.io.FileNotFoundException: d:\files\D:\AAA.zip (文件名、目录名或卷标语法不正确。)
java.io.FileOutputStream.open0(Native Method)
java.io.FileOutputStream.open(Unknown Source)
java.io.FileOutputStream.(Unknown Source)
java.io.FileOutputStream.(Unknown Source)
com.atguigu.fileupload.servlet.UploadServlet.doPost(UploadServlet.java:80)
javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Note The full stack trace of the root cause is available in the server logs.
Apache Tomcat/9.0.17
UploadServlet 如下:
package com.atsf.fileupload.servlet;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// 1. 得到 FileItem 的集合 items
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
// Set factory constraints
factory.setSizeThreshold(1024 * 500);
File tempDirectory = new File("d:\\tempDirectory");
factory.setRepository(tempDirectory);
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Set overall request size constraint
upload.setSizeMax(1024 * 1024 * 5);
// Parse the request
try {
List /* FileItem */items = upload.parseRequest(request);
// 2. 遍历 items:
for (FileItem item : items) {
// 若是一个一般的表单域, 打印信息
if (item.isFormField()) {
String name = item.getFieldName();
String value = item.getString();
System.out.println(name + ": " + value);
}
// 若是文件域则把文件保存到 d:\\files 目录下.
else {
String fieldName = item.getFieldName();
String fileName = item.getName();
String contentType = item.getContentType();
long sizeInBytes = item.getSize();
System.out.println(fieldName);
System.out.println(fileName);
System.out.println(contentType);
System.out.println(sizeInBytes);
InputStream in = item.getInputStream();
byte[] buffer = new byte[1024];
int len = 0;
fileName = "d:\\files\\" + fileName;
System.out.println(fileName);
OutputStream out = new FileOutputStream(fileName);
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
out.close();
in.close();
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}
}
}
解决了,因为浏览器上传路径不同导致,将文件名前面的路径截掉即可,如下代码:
fileName = fileName.substring(fileName.lastIndexOf("\")+1);