ConstraintChecker checker = (ConstraintChecker)servletContext.getAttribute(SC_CONSTRAINT_CHECKER);
String destUrl = config.getProperty(DEST_URL_PREFIX);
PathGenerator pathGenerator = (PathGenerator) servletContext.getAttribute(SC_PATH_GENERATOR);
ResourceBundle bundle = ResourceBundle.getBundle("messages", request.getLocale());
String type = request.getParameter("type");
String editorId = request.getParameter("editorid");
Collection<Part> parts = null;
try {
parts = request.getParts();
} catch (IllegalStateException e) {
String pattern = bundle.getString(MSG_UPLOAD_EXCEEDED);
out.println(buildResponseScript(editorId, destUrl,
MessageFormat.format(pattern,
config.getProperty(HUMAN_UPLOAD_FILE_SIZE_LIMIT),
config.getProperty(HUMAN_REQUEST_SIZE_LIMIT))));
return;
} catch (ServletException e) {
out.println(buildResponseScript(editorId, destUrl, bundle.getString(MSG_WRONG_ENCTYPE)));
return;
} catch (IOException e) {
out.println(buildResponseScript(editorId, destUrl, bundle.getString(MSG_IO_ERROR)));
return;
}
for (Part part : parts) {
String fileName = extractFileName(part);
if(fileName == null) continue;
// 检查扩展名
String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1)
.toLowerCase();
if( !checker.checkFileExtension(fileExt) ) {
out.println(buildResponseScript(editorId, destUrl, bundle.getString(MSG_EXT_VIOLATED)));
return;
}
String path = pathGenerator.generate(request, fileName);
int last = path.lastIndexOf(File.separator);
if(last > 0) {
File lastDir = new File(uploadRoot, path.substring(0, last));
if(!lastDir.exists())
lastDir.mkdirs();