struts糞?謹夕頭貧勧
扮寂:2010-12-26 BlogJava
package com.ninetowns.zhangc.struts.action;
public class Constant {
private Constant() {
}
public static Integer pictureSize = 10*1024*1024; //汽了葎byte 貧勧猟周恷寄?駆?
public static String pictureDirectory = "upload\\";
}
package com.ninetowns.zhangc.struts.action;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
import org.apache.struts.upload.MultipartRequestHandler;
public class PictureUpLoad {
private PictureUpLoad() {
}
private static boolean isValidFile(String fileName) {
String[] validFiles = { "gif", "jpg", "jpeg", "jpe", "bmp", "png","txt/plain","text","doc","rar" };
boolean ret = false;
for (int i = 0; i < validFiles.length; i++) {
if (fileName.toLowerCase().endsWith(validFiles[i])) {
ret = true;
break;
}
}
return ret;
}
private static String rebuildFileName(String fileName) {
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
"yyyyMMddHHmmss");
java.util.Date date = new java.util.Date();
return sdf.format(date) + "_" + fileName;
}
private static String lianjieFileName(String fileName){
return " "+fileName;
}
private static boolean isDirectoryExists(String path){
java.io.File file = new File(path);
if(!file.exists()){
return file.mkdir();
}else
return true;
}
private static String[] getFileName(String filesName){
Pattern x=Pattern.compile("\\w*\\s+\\w*");
String[] fileName = filesName.split("\\s+");
Matcher t = x.matcher(filesName);
System.out.println( t.matches());
return fileName;
}
public static String[] fileUpLoad(ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String path = "";
String fileName = "";
MultipartRequestHandler handle = form.getMultipartRequestHandler();
Hashtable hashTable = handle.getFileElements();
Collection cln = hashTable.values();
Iterator iterator = cln.iterator();
while (iterator.hasNext()) {
FormFile file = (FormFile) iterator.next();
path = request.getRealPath("/") + Constant.pict
|