?? uploadrule.java
字號:
package com.jeecms.core.util;
import static com.jeecms.core.Constants.SPT;
import java.io.File;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ponyjava.common.util.StrUtils;
/**
* 上傳規則定義類。
*
* 在上傳之前將上傳規則對象保存在session中,之后編輯器或其他上傳對象將根據上傳規則上傳文件。
*
* 編輯器瀏覽服務器的根路徑:rootPath。模板制作時需要指定根路徑,以便上傳圖片。
*
* 定義上傳路徑
*
* @author liufang
*
*/
public class UploadRule implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private static final Logger log = LoggerFactory.getLogger(UploadRule.class);
/**
* 在session中的key
*/
public static final String KEY = "_upload_rule";
/**
* 構造器
*
* @param rootPath
* 根路徑。瀏覽服務器的根路徑。上傳時可能需要再加上類別路徑
* @param pathPrefix
* 路徑前綴
* @param isGenName
* 是否創建隨機文件名
* @param hasType
* 是否區分文件類別。模板制作時不需要,其他情況下需要
* @param needClear
* 是否需要清除未使用的上傳文件
*/
public UploadRule(String rootPath, String pathPrefix, boolean isGenName,
boolean hasType, boolean needClear) {
this.rootPath = rootPath;
this.pathPrefix = pathPrefix;
this.isGenName = isGenName;
this.hasType = hasType;
this.needClear = needClear;
}
/**
* 構造器
*
* @param rootPath
* 根路徑。瀏覽服務器的根路徑。上傳時可能需要再加上類別路徑
* @param isGenName
* 是否創建隨機文件名
* @param hasType
* 是否區分文件類別。模板制作時不需要,其他情況下需要
*/
public UploadRule(String rootPath, String pathPrefix, boolean isGenName,
boolean hasType) {
this(rootPath, pathPrefix, isGenName, hasType, true);
}
/**
* 構造器
*
* @param rootPath
* 根路徑。瀏覽服務器的根路徑。上傳時可能需要再加上類別路徑
* @param isGenName
* 是否創建隨機文件名
*/
public UploadRule(String rootPath, String pathPrefix, boolean isGenName) {
this(rootPath, pathPrefix, isGenName, true, true);
}
/**
* 構造器
*
* @param rootPath
* 根路徑。瀏覽服務器的根路徑。上傳時可能需要再加上類別路徑
*/
public UploadRule(String rootPath, String pathPrefix) {
this(rootPath, pathPrefix, true, true, true);
}
/**
* 獲得文件全名
*
* 目錄前綴/年+季度/月+日/文件名.suffix
*
* @return
*/
public String getPathName(String fileName, String suffix, String type) {
StringBuilder sb = new StringBuilder(getPathPrefix()).append(type)
.append(genFilePath());
if (isGenName) {
sb.append(genFileName());
} else {
sb.append(fileName);
}
sb.append('.').append(suffix);
return sb.toString();
}
/**
* 按當前日期生產路徑:/2008_2/5_20/,/年_季/月_日/
*
* @return
*/
public static String genFilePath() {
StringBuilder sb = new StringBuilder();
Calendar cal = Calendar.getInstance();
sb.append(SPT).append(cal.get(Calendar.YEAR)).append('_').append(
cal.get((Calendar.MONTH)) / 3 + 1).append(SPT).append(
cal.get(Calendar.MONTH) + 1).append('_').append(
cal.get(Calendar.DAY_OF_MONTH)).append(SPT);
return sb.toString();
}
/**
* 獲得文件名
*
* 4位隨機數加上當前時間
*
* @return
*/
public static String genFileName() {
String name = StrUtils.longToN36(System.currentTimeMillis());
return RandomStringUtils.random(4, StrUtils.N36_CHARS) + name;
}
/**
* 獲得可圖片的后綴,如沒有指定,則使用默認的后綴集合。
*
* @return
*/
public Set<String> getAcceptImg() {
if (acceptImg == null) {
acceptImg = new HashSet<String>();
for (String s : DEF_IMG_ACCEPT) {
acceptImg.add(s);
}
}
return acceptImg;
}
public void addUploadFile(String origName, String fileName,
String realPath, long size) {
if (uploadFiles == null) {
uploadFiles = new HashMap<String, UploadFile>();
}
uploadFiles.put(fileName, new UploadFile(origName, fileName, realPath,
size));
}
public void removeUploadFile(String fileName) {
if (uploadFiles != null) {
uploadFiles.remove(fileName);
}
}
public Map<String, UploadFile> getUploadFiles() {
return uploadFiles;
}
public void clearUploadFile() {
if (uploadFiles != null && needClear) {
for (UploadFile uf : uploadFiles.values()) {
File file = new File(uf.getRealPath());
if (file.delete()) {
log.debug("刪除未被使用的文件:{}", file.getName());
} else {
log.warn("刪除文件失敗:{}", file.getName());
}
}
uploadFiles.clear();
}
}
/**
* 已經上傳到圖片
*/
private Map<String, UploadFile> uploadFiles;
/**
* 可以上傳的文件后綴
*/
private Set<String> acceptImg;
/**
* 編輯器瀏覽服務器的根路徑,也是上傳的根路徑
*/
private String rootPath;
private String pathPrefix;
/**
* 是否生成文件名
*/
private boolean isGenName = true;
/**
* 是否區分文件類型(用于編輯器瀏覽服務器時使用)
*/
private boolean hasType = true;
/**
* 是否需要清理
*/
private boolean needClear = true;
/**
* 是否允許瀏覽文件
*/
private boolean allowFileBrowsing = true;
/**
* 是否允許上傳文件
*/
private boolean allowUpload = true;
/**
* 允許上傳的大小。0不允許上傳,-1不受限制
*/
private int allowSize = -1;
/**
* 已上傳大小
*/
private int uploadSize = 0;
/**
* 默認的可上傳文件后綴
*/
public static final String[] DEF_IMG_ACCEPT = { "jpg", "gif", "jpeg",
"png", "bmp", };
public static void main(String[] args) {
UploadRule rule = new UploadRule("", "", true);
System.out.println(rule.getPathName("", "jpg", "img"));
}
public boolean isGenName() {
return isGenName;
}
public void setGenName(boolean isGenName) {
this.isGenName = isGenName;
}
public void setAcceptImg(Set<String> acceptImg) {
this.acceptImg = acceptImg;
}
public String getRootPath() {
return rootPath;
}
public void setRootPath(String rootPath) {
this.rootPath = rootPath;
}
public boolean isHasType() {
return hasType;
}
public void setHasType(boolean hasType) {
this.hasType = hasType;
}
public boolean isNeedClear() {
return needClear;
}
public void setNeedClear(boolean needClear) {
this.needClear = needClear;
}
public static class UploadFile implements java.io.Serializable {
private static final long serialVersionUID = 1L;
public UploadFile() {
}
public UploadFile(String origName, String fileName, String realPath,
long size) {
this.origName = origName;
this.fileName = fileName;
this.realPath = realPath;
this.size = size;
}
public String getRelPath(String pathRoot) {
String real = getRealPath();
real = StringUtils.replace(real, pathRoot, "");
real = StringUtils.replace(real, File.separator, "/");
return real;
}
private String origName;
private String fileName;
private String realPath;
private long size;
public String getOrigName() {
return origName;
}
public void setOrigName(String origName) {
this.origName = origName;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getRealPath() {
return realPath;
}
public void setRealPath(String realPath) {
this.realPath = realPath;
}
public long getSize() {
return size;
}
public void setSize(long size) {
this.size = size;
}
}
public String getPathPrefix() {
return pathPrefix;
}
public void setPathPrefix(String pathPrefix) {
this.pathPrefix = pathPrefix;
}
public boolean isAllowFileBrowsing() {
return allowFileBrowsing;
}
public void setAllowFileBrowsing(boolean allowFileBrowsing) {
this.allowFileBrowsing = allowFileBrowsing;
}
public int getUploadSize() {
return uploadSize;
}
public void setUploadSize(int uploadSize) {
this.uploadSize = uploadSize;
}
public boolean isAllowUpload() {
return allowUpload;
}
public void setAllowUpload(boolean allowUpload) {
this.allowUpload = allowUpload;
}
public int getAllowSize() {
return allowSize;
}
public void setAllowSize(int allowSize) {
this.allowSize = allowSize;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -