package
com.file.upload;
import
java.io.File;
import
java.util.Iterator;
import
java.util.List;
import
javax.servlet.http.HttpServletRequest;
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;
import
com.file.helper.FileConst;
import
com.file.helper.ReadUploadFileType;
public
class
FileUpload {
private
static
String uploadPath =
null
;
// 上傳文件的目錄
private
static
String tempPath =
null
;
// 臨時文件目錄
private
static
File uploadFile =
null
;
private
static
File tempPathFile =
null
;
private
static
int
sizeThreshold =
1024
;
private
static
int
sizeMax =
4194304
;
//初始化
static
{
sizeMax = Integer.parseInt(FileConst.getValue(
'sizeMax'
));
sizeThreshold = Integer.parseInt(FileConst.getValue(
'sizeThreshold'
));
uploadPath = FileConst.getValue(
'uploadPath'
);
uploadFile =
new
File(uploadPath);
if
(!uploadFile.exists()) {
uploadFile.mkdirs();
}
tempPath = FileConst.getValue(
'tempPath'
);
tempPathFile =
new
File(tempPath);
if
(!tempPathFile.exists()) {
tempPathFile.mkdirs();
}
}
/**
* 上傳文件
* @param request
* @return true 上傳成功 false上傳失敗
*/
@SuppressWarnings
(
'unchecked'
)
public
static
boolean
upload(HttpServletRequest request){
boolean
flag =
true
;
//檢查輸入請求是否為multipart表單數(shù)據(jù)。
boolean
isMultipart = ServletFileUpload.isMultipartContent(request);
//若果是的話
if
(isMultipart){
/**為該請求創(chuàng)建一個DiskFileItemFactory對象,通過它來解析請求。執(zhí)行解析后,所有的表單項目都保存在一個List中。**/
try
{
DiskFileItemFactory factory =
new
DiskFileItemFactory();
factory.setSizeThreshold(sizeThreshold);
// 設置緩沖區(qū)大小,這里是4kb
factory.setRepository(tempPathFile);
// 設置緩沖區(qū)目錄
ServletFileUpload upload =
new
ServletFileUpload(factory);
upload.setHeaderEncoding(
'UTF-8'
);
//解決文件亂碼問題
upload.setSizeMax(sizeMax);
// 設置最大文件尺寸
List<FileItem> items = upload.parseRequest(request);
// 檢查是否符合上傳的類型
if
(!checkFileType(items))
return
false
;
Iterator<FileItem> itr = items.iterator();
//所有的表單項
//保存文件
while
(itr.hasNext()){
FileItem item = (FileItem) itr.next();
//循環(huán)獲得每個表單項
if
(!item.isFormField()){
//如果是文件類型
String name = item.getName();
//獲得文件名 包括路徑啊
if
(name!=
null
){
File fullFile=
new
File(item.getName());
File savedFile=
new
File(uploadPath,fullFile.getName());
item.write(savedFile);
}
}
}
}
catch
(FileUploadException e) {
flag =
false
;
e.printStackTrace();
}
catch
(Exception e) {
flag =
false
;
e.printStackTrace();
}
}
else
{
flag =
false
;
System.out.println(
'the enctype must be multipart/form-data'
);
}
return
flag;
}
/**
* 刪除一組文件
* @param filePath 文件路徑數(shù)組
*/
public
static
void
deleteFile(String [] filePath){
if
(filePath!=
null
&& filePath.length>
0
){
for
(String path:filePath){
String realPath = uploadPath+path;
File delfile =
new
File(realPath);
if
(delfile.exists()){
delfile.delete();
}
}
}
}
/**
* 刪除單個文件
* @param filePath 單個文件路徑
*/
public
static
void
deleteFile(String filePath){
if
(filePath!=
null
&& !
''
.equals(filePath)){
String [] path =
new
String []{filePath};
deleteFile(path);
}
}
/**
* 判斷上傳文件類型
* @param items
* @return
*/
private
static
Boolean checkFileType(List<FileItem> items){
Iterator<FileItem> itr = items.iterator();
//所有的表單項
while
(itr.hasNext()){
FileItem item = (FileItem) itr.next();
//循環(huán)獲得每個表單項
if
(!item.isFormField()){
//如果是文件類型
String name = item.getName();
//獲得文件名 包括路徑啊
if
(name!=
null
){
File fullFile=
new
File(item.getName());
boolean
isType = ReadUploadFileType.readUploadFileType(fullFile);
if
(!isType)
return
false
;
break
;
}
}
}
return
true
;
}
}