package com.brgroup.util;
import grade.entity.Grade;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
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;
public class ImpFactory {
public static Grade addImp(HttpServletRequest request) {
DiskFileItemFactory disk = new DiskFileItemFactory();
ServletFileUpload s = new ServletFileUpload(disk);
s.setSizeMax(1024 * 400);// 你可以上传的最大图片是多少
Grade grade = new Grade();
try {
List<FileItem> list = s.parseRequest(request);
for (FileItem item : list) {
if (!item.isFormField()) {// 判断你的input的类型是不是file,如果不是file为treu,反之为false
String path = "D:\\用户目录\\我的文档\\Tencent Files\\753680745\\FileRecv\\apache-tomcat-6.0.20\\apache-tomcat-6.0.20\\webapps\\student\\img";
String fileName = "imge/" + imgName() + "."
+ imgSuffix(item.getName());
File file = new File(path + "\\" + fileName);
if (!file.exists()) {// 这个文件是否存在
item.write(file);
}
grade.setGradeLoge(fileName);
}else{
String number = item.getFieldName();
String value = item.getString();
if(number.equals("id")&&value.length()>0)
grade.setId(Integer.parseInt(new String(value.getBytes("ISO8859-1"),"utf-8")));
else if(item.getFieldName().equals("gradeName"))
grade.setGradeName(new String(value.getBytes("ISO8859-1"),"utf-8"));
}
}
} catch (FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return grade;
}
public static String imgName() {
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
int a = (int) (Math.random() * 1000);
return format.format(new Date()) + a;
}
public static String imgSuffix(String fileName) {
String a[] = fileName.split("\\.");
return a[1];
}
public static void main(String[] args) {
System.out.println(imgSuffix("123.png"));
}
}