<span style="font-family: Arial, Helvetica, sans-serif; ">
</span>
<span style="font-family: Arial, Helvetica, sans-serif; "><span style="color:#ff0000;">一 环境的搭建:</span></span>
eclipase + javaEE + commons-fileupload-1.2.1.jar + commons-io-2.0.jar + tomcat 7.0.68
二 文件上传的servlet 代码实现!
package com.baidu.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.annotation.WebServlet;
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;
@WebServlet("/uploadServlet")
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(1024*500);
File tempDirectory = new File("d:\\tempDirectory");
factory.setRepository(tempDirectory);
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List<FileItem> items = upload.parseRequest(request);
for(FileItem item:items){
if(item.isFormField()){
String name = item.getFieldName();
String value = item.getString();
System.out.println( name + " : " + value);
}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();
}
}
}
三 JSP 编写
update.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="uploadServlet" method="post" enctype="multipart/form-data">
File: <input type="file" name="file" />
<br><br>
<br><br>
DESC: <input type="text" name="desc" />
<br><br>
<br><br>
<input type="submit" value="Submit" />
</form>
</body>
</html>
四, 运行过程中出现的问题
五, 解决的办法:
进过一番百度, 无意中看到一位朋友提到,不同的浏览器对上传的文件名的处理方式不太一样 ,进过一番实验,搞定,且增加了一行代码!
package com.baidu.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.annotation.WebServlet;
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;
@WebServlet("/uploadServlet")
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(1024*500);
File tempDirectory = new File("d:\\tempDirectory");
factory.setRepository(tempDirectory);
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(1024 * 1024 * 5);
try {
List<FileItem> items = upload.parseRequest(request);
for(FileItem item:items){
if(item.isFormField()){
String name = item.getFieldName();
String value = item.getString();
System.out.println( name + " : " + value);
}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);
//不同的浏览器提交的文件名是不一样的,有些浏览器提交的文件名是带有路径的,如:c:\nihao\a.txt,而有些只是单纯的文件名,如:a.txt
//处理获取到的上传文件的文件名的路径部分,只保留文件名部分
fileName = fileName.substring(fileName.lastIndexOf("\\")+1);
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();
}
}
}
显示推荐内容