/**
* 这是一个辅助类,辅助完成上传功能。
* 可以选择将文件保存在数据库里或保存在文件系统上
* 并对文件的类型和大小进行了限制
*/
package com.hywavesoft.struts.commons;
import java.io.*;
public class UploadUtil {
private static final String DATABASE_DEST = "database";
private static final String FILE_DEST = "file";
private static final int MAX_SIZE = 1024 * 1024;
private static final String[] TYPES = { ".jpg", ".gif", ".zip", ".rar" };
public static void saveFile(String fileName, byte[] fileData, int size,
String dest) throws FileNotFoundException, IOException {
if (!checkSize(size)) {
throw new IOException(size + " is too large !");
}
if (!checkType(fileName)) {
throw new IOException("Unvaildate type !");
}
if (dest.equals(DATABASE_DEST)) {
saveToDb(fileName, fileData);
}
if (dest.equals(FILE_DEST)) {
saveToFile(fileName, fileData);
}
}
private static void saveToDb(String fileName, byte[] fileData) {
}
private static void saveToFile(String fileName, byte[] fileData)
throws FileNotFoundException, IOException {
OutputStream o = new FileOutputStream(fileName);
o.write(fileData);
o.close();
}
public static void delFile(String fileName, String dest)
throws NullPointerException, SecurityException {
if (dest.equals(DATABASE_DEST)) {
delFromDb(fileName);
}
if (dest.equals(FILE_DEST)) {
delFromFile(fileName);
}
}
private static void delFromDb(String fileName) {
}
private static void delFromFile(String fileName)
throws NullPointerException, SecurityException {
File file = new File(fileName);
if (file.exists())
file.delete();
}
private static boolean checkSize(int size) {
if (size > MAX_SIZE)
return false;
return true;
}
private static boolean checkType(String fileName) {
for (int i = 0; i < TYPES.length; i++) {
if (fileName.toLowerCase().endsWith(TYPES[i])) {
return true;
}
}
return false;
}
} |