?? fileutil.java
字號:
package com.cnpoint.myspaces.common.util;
import java.io.File;
import java.io.InputStream;
import org.apache.commons.fileupload.*;
import org.apache.commons.logging.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.*;
public class FileUtil {
private static Log log = LogFactory.getLog(FileUtil.class);
// 當上傳文件超過限制時設定的臨時文件位置
private String tempPath = "C:\\TEMP";
// 文件上傳目標目錄
private String destinationPath;
// 獲取的上傳請求
private HttpServletRequest fileuploadRequest = null;
// 設置最多只允許在內存中存儲的數據,單位:字節
//private int sizeThreshold = 4096;
private int sizeThreshold = 4096000;
// 設置允許用戶上傳文件大小,單位:字節
// 共10M
private long sizeMax = 10*10485760;
//private long sizeMax = 40960000 ;
//private long sizeMax = -1;
String fileName = null;
public FileUtil(){
}
public FileUtil(String tempPath, String destinationPath){
this.tempPath = tempPath;
this.destinationPath = destinationPath;
}
public FileUtil(String tempPath, String destinationPath, HttpServletRequest fileuploadRequest){
this.tempPath = tempPath;
this.destinationPath = destinationPath;
this.fileuploadRequest = fileuploadRequest;
}
/** 文件上載
* @return true ?? success; false ?? fail.
*/
public boolean Upload(){
// 如果沒有臨時目錄,則創建它
if(!(new File(tempPath).isDirectory())){
try{
new File(tempPath).mkdirs();
}catch(SecurityException e){
//System.out.println("can not make security direcoty");
log.info(e);
}
}
// 如果沒有上傳目的目錄,則創建它
if(!(new File(destinationPath).isDirectory())){
try{
new File(destinationPath).mkdirs();
}catch(SecurityException e){
System.out.println("can not make security direcoty");
}
}
// 上傳項目只要足夠小,就應該保留在內存里。
// 較大的項目應該被寫在硬盤的臨時文件上。
// 非常大的上傳請求應該避免。
// 限制項目在內存中所占的空間,限制最大的上傳請求,并且設定臨時文件的位置。
DiskFileUpload fu = new DiskFileUpload();
// 設置最多只允許在內存中存儲的數據,單位:字節
fu.setSizeThreshold(sizeThreshold);
// 設置允許用戶上傳文件大小,單位:字節
// 10M
fu.setSizeMax(sizeMax);
// 設置一旦文件大小超過getSizeThreshold()的值時數據存放在硬盤的目錄
fu.setRepositoryPath(tempPath);
Iterator iter = null;
// 讀取上傳信息
try {
List fileItems = fu.parseRequest(fileuploadRequest);
// 處理上傳項目
// 依次處理每個上傳的文件
iter = fileItems.iterator();
} catch (FileUploadException e) {
log.info("上傳文件過大");
//e.printStackTrace();
log.info(e.getMessage());
return false;
}
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
// 忽略其他不是文件域的所有表單信息
if (!item.isFormField()) {
// 上傳的是文件信息
//String fieldName = item.getFieldName();
String name = item.getName();
if((name == null) || name.equals("") && item.getSize() == 0){
continue;
}
fileName = this.GetFileName(name);
setFileName(System.currentTimeMillis()+"-"+fileName);
try {
//FileOutputStream fos = new FileOutputStream(new File(this.destinationPath + fileName));
FileOutputStream fos = new FileOutputStream(new File(this.destinationPath + getFileName() ));
InputStream uploadStream = item.getInputStream();
BufferedInputStream bis = new BufferedInputStream(uploadStream);
byte b[] = new byte[409600];
int nRead;
while( (nRead = bis.read(b,0,409600)) >0){
fos.write(b, 0, nRead);
}
uploadStream.close();
bis.close();
fos.close();
} catch (Exception e) {
//e.printStackTrace();
log.info(e.getMessage());
return false;
}
}else{
// 上傳的是普通表單字域
String fieldName = item.getFieldName();
String name = item.getName();
//System.out.println(fieldName);
if(fieldName.equals("docID")){
setDocID(item.getString());
}
if(fieldName.equals("docName")){
setDocName(item.getString());
}
if(fieldName.equals("curedition")){
setCuredition(item.getString());
}
if((name == null) || name.equals("") && item.getSize() == 0){
continue;
}
}
}
return true;
}
/**從路徑中獲取單獨文件名
* @author Huangye
*
* 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;
}
/**
* 省略所有的getXXX和setXXX訪問器方法
*/
public String getFileName(){
return fileName;
}
public void setFileName(String fileName){
this.fileName = fileName;
}
String docName;
String docID;
String curedition;
public String getCuredition() {
return curedition;
}
public void setCuredition(String curedition) {
this.curedition = curedition;
}
public String getDocID() {
return docID;
}
public void setDocID(String docID) {
this.docID = docID;
}
public String getDocName() {
return docName;
}
public void setDocName(String docName) {
this.docName = docName;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -