import com.qeweb.util.upload.SingleFileUpload;
//上传文件
SingleFileUpload upload=new SingleFileUpload();
upload.setSizeMax(1024*1024);//设置上传文件大小上限为1M
try {
upload.parseRequest(request);
} catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
}
String fileName=upload.getFileItem().getName();
if (fileName==null&&fileName.length()==0) {
return mapping.getInputForward();
}
ActionMessages msgs = new ActionMessages();
String[] allowedSuffix={"gif","jpg"};
String ext=FileHelper.extractFileExt(fileName, true);
//判断文件类型是否有效
boolean flag=false;
for (int i = 0; i < allowedSuffix.length; i++) {
if (FileHelper.isValidFileExt(fileName, allowedSuffix[i])) {
flag=true;
break;
}
}
if (!flag) {
addActionMessageX(WebConstants.ERROR, request, msgs, "label.systemmgt.sysorganizationmaint.fileExtensionerror");
saveMessages(request, msgs);
logMsg = "上传文件类型错误";
return mapping.getInputForward();
}
//文件路径
String orgID=RightsSessionDataHelper.getCurrentOrganizationId(request).toString();
String fileServerPath=WebUtilInternal.getRealPath(request, UPLOAD_FOLDER)+orgID+"/"+"logo."+ext;
FileHelper.makeSureDirExist(fileServerPath);
File tempFile=new File(fileServerPath);
File parentFile=tempFile.getParentFile();
File[]lists=parentFile.listFiles();
for (int i = 0; i < lists.length&&parentFile.isDirectory(); i++) {
lists[i].delete();
}
try {
upload.upload(fileServerPath);
} catch (Exception ex) {
logMsg = "上传文件失败";
log.error("Could not upload file to "+fileServerPath,ex);
return mapping.getInputForward();
}
setOptions(request);
//success
return mapping.findForward("load");