use of org.apache.commons.fileupload.disk.DiskFileItemFactory in project MSEC by Tencent.
the class FileUploadServlet method FileUpload.
// 处理multi-part格式的http请求
// 将key-value字段放到fields里返回
// 将文件保存到tmp目录,并将文件名保存到filesOnServer列表里返回
protected static String FileUpload(Map<String, String> fields, List<String> filesOnServer, HttpServletRequest request, HttpServletResponse response) {
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
int MaxMemorySize = 200000000;
int MaxRequestSize = MaxMemorySize;
String tmpDir = System.getProperty("TMP", "/tmp");
factory.setSizeThreshold(MaxMemorySize);
factory.setRepository(new File(tmpDir));
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("utf8");
upload.setSizeMax(MaxRequestSize);
try {
List<FileItem> items = upload.parseRequest(request);
// Process the uploaded items
Iterator<FileItem> iter = items.iterator();
while (iter.hasNext()) {
FileItem item = iter.next();
if (item.isFormField()) {
// 普通的k -v字段
String name = item.getFieldName();
String value = item.getString("utf-8");
fields.put(name, value);
} else {
String fieldName = item.getFieldName();
String fileName = item.getName();
if (fileName == null || fileName.length() < 1) {
return "file name is empty.";
}
String localFileName = ServletConfig.fileServerRootDir + File.separator + "tmp" + File.separator + fileName;
String contentType = item.getContentType();
boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize();
File uploadedFile = new File(localFileName);
item.write(uploadedFile);
filesOnServer.add(localFileName);
}
}
return "success";
} catch (FileUploadException e) {
e.printStackTrace();
return e.getMessage();
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
}
use of org.apache.commons.fileupload.disk.DiskFileItemFactory in project MSEC by Tencent.
the class FileUploadServlet method FileUpload.
// 处理multi-part格式的http请求
// 将key-value字段放到fields里返回
// 将文件保存到tmp目录,并将文件名保存到filesOnServer列表里返回
protected static String FileUpload(Map<String, String> fields, List<String> filesOnServer, HttpServletRequest request, HttpServletResponse response) {
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
int MaxMemorySize = 10000000;
int MaxRequestSize = MaxMemorySize;
String tmpDir = System.getProperty("TMP", "/tmp");
// System.out.printf("temporary directory:%s", tmpDir);
// Set factory constraints
factory.setSizeThreshold(MaxMemorySize);
factory.setRepository(new File(tmpDir));
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("utf8");
// Set overall request size constraint
upload.setSizeMax(MaxRequestSize);
// Parse the request
try {
@SuppressWarnings("unchecked") List<FileItem> items = upload.parseRequest(request);
// Process the uploaded items
Iterator<FileItem> iter = items.iterator();
while (iter.hasNext()) {
FileItem item = iter.next();
if (item.isFormField()) {
// 普通的k -v字段
String name = item.getFieldName();
String value = item.getString("utf-8");
fields.put(name, value);
} else {
String fieldName = item.getFieldName();
String fileName = item.getName();
if (fileName == null || fileName.length() < 1) {
return "file name is empty.";
}
String localFileName = ServletConfig.fileServerRootDir + File.separator + "tmp" + File.separator + fileName;
// System.out.printf("upload file:%s", localFileName);
String contentType = item.getContentType();
boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize();
File uploadedFile = new File(localFileName);
item.write(uploadedFile);
filesOnServer.add(localFileName);
}
}
return "success";
} catch (FileUploadException e) {
e.printStackTrace();
return e.toString();
} catch (Exception e) {
e.printStackTrace();
return e.toString();
}
}
use of org.apache.commons.fileupload.disk.DiskFileItemFactory in project zm-mailbox by Zimbra.
the class FileUploadServlet method getUploader.
public static ServletFileUpload getUploader(long maxSize) {
DiskFileItemFactory dfif = new DiskFileItemFactory();
dfif.setSizeThreshold(32 * 1024);
dfif.setRepository(new File(getUploadDir()));
ServletFileUpload upload = new ServletFileUpload(dfif);
upload.setSizeMax(maxSize);
upload.setHeaderEncoding("utf-8");
return upload;
}
use of org.apache.commons.fileupload.disk.DiskFileItemFactory in project MassBank-web by MassBank.
the class AdminActions method doUpload.
@Action(name = "doUpload", post = true)
public Redirect doUpload(HttpServletRequest req) throws ServletException {
RequestContext reqContext = new ServletRequestContext(req);
boolean isMultipart = ServletFileUpload.isMultipartContent(reqContext);
if (isMultipart) {
try {
// Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
List<?> items = upload.parseRequest(req);
// Process the uploaded items
Iterator<?> iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (!item.isFormField()) {
String fileName = item.getName();
String fileExtesion = fileName;
fileExtesion = fileExtesion.toLowerCase();
if (!(fileExtesion.endsWith(".jar") || fileExtesion.endsWith(".aar"))) {
return new Redirect(UPLOAD).withStatus(false, "Unsupported file type " + fileExtesion);
} else {
String fileNameOnly;
if (fileName.indexOf("\\") < 0) {
fileNameOnly = fileName.substring(fileName.lastIndexOf("/") + 1, fileName.length());
} else {
fileNameOnly = fileName.substring(fileName.lastIndexOf("\\") + 1, fileName.length());
}
File uploadedFile = new File(serviceDir, fileNameOnly);
item.write(uploadedFile);
return new Redirect(UPLOAD).withStatus(true, "File " + fileNameOnly + " successfully uploaded");
}
}
}
} catch (Exception e) {
return new Redirect(UPLOAD).withStatus(false, "The following error occurred: " + e.getMessage());
}
}
throw new ServletException("Invalid request");
}
use of org.apache.commons.fileupload.disk.DiskFileItemFactory in project mica2 by obiba.
the class DraftStudiesImportResource method getUploadedFile.
/**
* Returns the first {@code FileItem} that is represents a file upload field. If no such field exists, this method
* returns null
*
* @param request
* @return
* @throws FileUploadException
*/
FileItem getUploadedFile(HttpServletRequest request) throws FileUploadException {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
for (FileItem fileItem : upload.parseRequest(request)) {
if (!fileItem.isFormField()) {
return fileItem;
}
}
return null;
}
Aggregations