package com.peng.web;
import java.io.BufferedInputStream;
import
java.io.BufferedOutputStream;
import java.io.File;
import
java.io.FileInputStream;
import java.io.FileOutputStream;
import
java.io.IOException;
import java.io.InputStream;
import
java.util.List;
import java.util.UUID;
import javax.servlet.ServletException;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import org.apache.commons.codec.digest.DigestUtils;
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;
@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest
request, HttpServletResponse response)
throws ServletException,
IOException {
//上传路径
File file =new
File("E:/upload");
//上传临时路径
File tFile = new
File("E:/tempupload");
//判断文件夹是否存在,如果不存在就创建
if (!file.exists())
{
file.mkdir();
}
//判断临时文件夹是否存在,如果不存在就创建
if (!tFile.exists())
{
tFile.mkdir();
}
//判断form中enctype="multipart/form-data"有没有写
if
(ServletFileUpload.isMultipartContent(request)) {
DiskFileItemFactory
factory = new
DiskFileItemFactory();
//设置缓冲区大小
factory.setSizeThreshold(10*1024);
//设置临时文件夹
factory.setRepository(tFile);
ServletFileUpload
upload = new
ServletFileUpload(factory);
//设置文件上传的大小
upload.setSizeMax(10*1024*1024);
try
{
//获取表单元素传过来的值
List<FileItem> fileItems =
upload.parseRequest(request);
for(FileItem item : fileItems){
if
(item.isFormField())
{
//普通的表单元素
item.getFieldName();//获取表单的name
item.getString();//获取表单的值
}else{
//文件元素,进行io操作
item.getName();//上传文件的文件名
item.getFieldName();//获取文件的name属性
item.getContentType();//获取文件的类型
item.getSize();//获取文件的大小
InputStream
inputStream =
item.getInputStream();
//根据MD5的值判断文件系统中是否存在被上传的文件
String path =
haveFile(inputStream);
if(path != null)
{
System.out.println("文件已经存在:" + path);
} else
{
BufferedInputStream bis = new
BufferedInputStream(item.getInputStream());
String newFileName =
getFileNewName(item.getName());
BufferedOutputStream bos = new
BufferedOutputStream(new FileOutputStream(new
File(file,newFileName)));
byte[] buffer = new
byte[1024];
int len = -1;
while((len = bis.read(buffer))
!= -1) {
bos.write(buffer, 0,
len);
}
bos.flush();
bos.close();
bis.close();
}
}
}
}
catch (FileUploadException e)
{
e.printStackTrace();
}
}
}
//防止名字重复
private
String getFileNewName(String name) {
String extName =
name.substring(name.lastIndexOf("."));
UUID uuid =
UUID.randomUUID();
String newName = uuid.toString() + extName;
return
newName;
}
//查看是否有相同的文件
public String haveFile(InputStream
inputStream) {
String path = null;
try {
String md5 =
DigestUtils.md5Hex(inputStream);
File file = new
File("E:/upload");
File[] files = file.listFiles();
for(File subFile
: files) {
InputStream ins = new FileInputStream(subFile);
String
subFileMd5 = DigestUtils.md5Hex(ins);
if(md5.equals(subFileMd5))
{
path = subFile.getPath();
}
}
} catch
(IOException e) {
e.printStackTrace();
}
return
path;
}
}