String operator = request.getParameter("operator"); //页面跳转的标识
String succeed="0";//记录是否上传成功
String fileName = "";//文件名字
String fileExt = "";
String filename1="";
if(operator!=null){
succeed="1";
//DiskFileItem工厂,主要用来设定上传文件的参数
org.apache.commons.fileupload.disk.DiskFileItemFactory factory = new org.apache.commons.fileupload.disk.DiskFileItemFactory(); factory.setSizeThreshold(60240); //设置内存缓冲区的阀值
// 使用fileItemFactory为参数实例化一个ServletFileUpload对象
org.apache.commons.fileupload.servlet.ServletFileUpload upload = new org.apache.commons.fileupload.servlet.ServletFileUpload(factory);
//upload.setHeaderEncoding(getEncoding()); //设置编码格式,推荐 jsp 和 处理类 均为 UTF-8
long max = 100L * 1024 * 1024;
upload.setSizeMax(max);//文件最大大小,字节
java.util.Map parameters = new java.util.HashMap();
java.util.Map files = new java.util.HashMap();
java.util.List items = upload.parseRequest(request);
java.util.Iterator it = items.iterator();
while (it.hasNext()) {
org.apache.commons.fileupload.FileItem item = (org.apache.commons.fileupload.FileItem) it.next();
if (item.isFormField()) { //如果是表单字段
String name = item.getFieldName();
String value = item.getString();
parameters.put(name, value);
} else { //如果是文件字段
String name = item.getFieldName();
files.put(name, item);
}
}
org.apache.commons.fileupload.FileItem baseitem = (org.apache.commons.fileupload.FileItem)files.get("ywFile");
if(null!=baseitem){
fileName=baseitem.getName();
int a=fileName.lastIndexOf("\\");
filename1=fileName.substring(a+1,fileName.length());
fileExt=com.saneis.utils.IOUtils.getExtension(baseitem.getName());
String absPath=new java.io.File(application.getRealPath(request.getRequestURI())).getParent();
java.io.File file=new java.io.File(absPath+"/image/"+filename1);
if(file.exists()){
filename1=filename1.split(".")[0]+"[1]"+filename1.split(".")[1];
file=new java.io.File(absPath+"/image/"+filename1);
}else{
return;
}
baseitem.write(new java.io.File(absPath+"/image/"+filename1)); succeed="2";
}
}