错误信息如下:
HTTP Status 500 – Internal Server Error
Type Exception ReportMessage 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.<init>(Unknown Source)
java.io.FileOutputStream.<init>(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.17UploadServlet 代码如下:
package com.atguigu.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> /* 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();
} }}查了类似错误,都是没有判断:是否创建路径(如下代码),添加了if(!tempDirectory .exists()){
tempDirectory .mkdirs();
}最终如下,但还是报错。
[code=java]
File tempDirectory = new File("d:\\tempDirectory");
if(!tempDirectory .exists()){
tempDirectory .mkdirs();
}
factory.setRepository(tempDirectory);[/code]
解决方案 »
- 请各位大侠帮忙看看··我的JSP+JBean的猜数字程序有什么问题没?为什么我运行起来是500错误!
- 急~~~如何用java把数据库数据导出到固定目录下?
- 跳转路径取值的问题!!
- 急急急急急急~~!~!!!在线等。。。tomcat下mysql连接问题。请大家帮帮忙。。。
- 怎样获取地址栏中的地址,在JSP中?在线等.
- 一个关于odbc数据源的问题,请各位多指教!!!嘿嘿
- 请教如果在jsp中读取客户端的excel文件中的数据?急呀!高手们救命呀!
- 在jsp页面中如何使页面打开时光标自动定位到某个输入项上??
- 弹出一个对话框,怎么样才能做到始终在最前面呢?
- 要实现以下界面需要怎么做
- jsp,java
- jsp
fileName = fileName.substring(fileName.lastIndexOf("\")+1);感谢各位朋友。