package com.gootrip.util;
import java.io.File;
import java.util.*;
import org.apache.commons.fileupload.*;
import javax.servlet.http.HttpServletRequest;
import java.util.regex.Pattern;
import java.io.IOException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import java.util.regex.Matcher;
public class FileUploadUtil {
private String tempPath =
null;
private String dstPath =
null;
private String newFileName =
null;
private HttpServletRequest fileuploadReq =
null;
private int sizeThreshold =
4096;
private long sizeMax =
10485760;
private int picSeqNo =
1;
private boolean isSmallPic =
false;
public FileUploadUtil(){
}
public FileUploadUtil(String tempPath, String destinationPath){
this.tempPath = tempPath;
this.dstPath = destinationPath;
}
public FileUploadUtil(String tempPath, String destinationPath, HttpServletRequest fileuploadRequest){
this.tempPath = tempPath;
this.dstPath = destinationPath;
this.fileuploadReq = fileuploadRequest;
}
/** 文件上载
* @return true —— success; false —— fail.
*/
public boolean Upload(){
DiskFileItemFactory factory =
new DiskFileItemFactory();
try {
FileUtil.makeDirectory(dstPath+
"/ddd");
FileUtil.makeDirectory(tempPath+
"/ddd");
factory.setSizeThreshold(sizeThreshold);
factory.setRepository(
new File(tempPath));
ServletFileUpload upload =
new ServletFileUpload(factory);
upload.setSizeMax(sizeMax);
List fileItems = upload.parseRequest(fileuploadReq);
Iterator iter = fileItems.iterator();
String regExp =
".+\\\\(.+)$";
String[] errorType = {
".exe",
".com",
".cgi",
".asp",
".php",
".jsp"};
Pattern p = Pattern.compile(regExp);
while (iter.hasNext()) {
System.out.println(
"++00++====="+newFileName);
FileItem item = (FileItem) iter.next();
if (!item.isFormField()) {
String name = item.getName();
System.out.println(
"++++====="+name);
long size = item.getSize();
if ((name ==
null || name.equals(
"")) && size ==
0)
continue;
Matcher m = p.matcher(name);
boolean result = m.find();
if (result) {
for (
int temp =
0; temp < errorType.length; temp++) {
if (m.group(
1).endsWith(errorType[temp])) {
throw new IOException(name +
": Wrong File Type");
}
}
String ext =
"."+FileUtil.getTypePart(name);
try {
if (newFileName ==
null || newFileName.trim().equals(
""))
{
item.write(
new File(dstPath +
"/"+ m.group(
1)));
}
else
{
String uploadfilename =
"";
if (isSmallPic)
{
uploadfilename = dstPath +
"/"+ newFileName+
"_"+picSeqNo+
"_small"+ext;
}
else
{
uploadfilename = dstPath +
"/"+ newFileName+
"_"+picSeqNo+ext;
}
System.out.println(
"++++====="+uploadfilename);
FileUtil.makeDirectory(uploadfilename);
item.write(
new File(uploadfilename));
}
picSeqNo++;
}
catch (Exception e) {
throw new IOException(e.getMessage());
}
}
else {
throw new IOException(
"fail to upload");
}
}
}
}
catch (IOException e) {
System.out.println(e);
}
catch (FileUploadException e) {
System.out.println(e);
}
return true;
}
/**从路径中获取单独文件名
* @author
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public String
GetFileName(String filepath)
{
String returnstr =
"*.*";
int length = filepath.trim().length();
filepath = filepath.replace(
'\\',
'/');
if(length >
0)
{
int i = filepath.lastIndexOf(
"/");
if (i >=
0)
{
filepath = filepath.substring(i +
1);
returnstr = filepath;
}
}
return returnstr;
}
/**
* 设置临时存贮目录
*/
public void setTmpPath(String tmppath)
{
this.tempPath = tmppath;
}
/**
* 设置目标目录
*/
public void setDstPath(String dstpath) {
this.dstPath = dstpath;
}
/**
* 设置最大上传文件字节数,不设置时默认10M
*/
public void setFileMaxSize(
long maxsize) {
this.sizeMax = maxsize;
}
/**
* 设置Http 请求参数,通过这个能数来获取文件信息
*/
public void setHttpReq(HttpServletRequest httpreq) {
this.fileuploadReq = httpreq;
}
/**
* 设置Http 请求参数,通过这个能数来获取文件信息
*/
public void setNewFileName(String filename) {
this.newFileName = filename;
}
/**
* 设置此上传文件是否是缩略图文件,这个参数主要用于缩略图命名
*/
public void setIsSmalPic(
boolean isSmallPic) {
this.isSmallPic = isSmallPic;
}
/**
* 设置Http 请求参数,通过这个能数来获取文件信息
*/
public void setPicSeqNo(
int seqNo) {
this.picSeqNo = seqNo;
}
}
转载于:https://www.cnblogs.com/bilaisheng/p/10210972.html
相关资源:UploadUtils