java文件上传实例_java 8 上传文件小例子报错!

查了类似错误,都是没有判断:是否创建路径(如下代码),添加了

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值