use of org.apache.commons.fileupload.servlet.ServletFileUpload in project eweb4j-framework by laiweiwei.
the class UploadUtil method handleUpload.
public static void handleUpload(Context context) throws Exception {
ConfigBean cb = (ConfigBean) SingleBeanCache.get(ConfigBean.class.getName());
UploadConfigBean ucb = cb.getMvc().getUpload();
String tmpDir = ucb.getTmp();
int memoryMax = CommonUtil.strToInt(CommonUtil.parseFileSize(ucb.getMaxMemorySize()) + "");
long sizeMax = CommonUtil.parseFileSize(ucb.getMaxRequestSize());
if (tmpDir.trim().length() == 0)
tmpDir = "${RootPath}" + File.separator + "WEB-INF" + File.separator + "tmp";
tmpDir = tmpDir.replace("${RootPath}", ConfigConstant.ROOT_PATH);
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(memoryMax);
factory.setRepository(new File(tmpDir));
ServletFileUpload _upload = new ServletFileUpload(factory);
if (!_upload.isMultipartContent(context.getRequest()))
return;
_upload.setSizeMax(sizeMax);
try {
List<FileItem> items = _upload.parseRequest(context.getRequest());
Iterator<FileItem> it = items.iterator();
while (it.hasNext()) {
FileItem item = it.next();
String fieldName = item.getFieldName();
if (item.isFormField()) {
String value = item.getString();
context.getQueryParamMap().put(fieldName, new String[] { value });
} else {
String fileName = item.getName();
if (fileName == null || fileName.trim().length() == 0)
continue;
String stamp = CommonUtil.getNowTime("yyyyMMddHHmmss");
File tmpFile = new File(tmpDir + File.separator + stamp + "_" + fileName);
item.write(tmpFile);
UploadFile uploadFile = new UploadFile(tmpFile, fileName, fieldName, item.getSize(), item.getContentType());
if (context.getUploadMap().containsKey(fieldName)) {
context.getUploadMap().get(fieldName).add(uploadFile);
} else {
List<UploadFile> uploads = new ArrayList<UploadFile>();
uploads.add(uploadFile);
context.getUploadMap().put(fieldName, uploads);
}
}
}
} catch (InvalidContentTypeException e) {
throw new Exception("upload file error", e);
}
}
use of org.apache.commons.fileupload.servlet.ServletFileUpload in project JessMA by ldcsaa.
the class FileUploader method getFileUploadComponent.
private ServletFileUpload getFileUploadComponent() {
DiskFileItemFactory dif = new DiskFileItemFactory();
if (factorySizeThreshold != DEFAULT_SIZE_THRESHOLD)
dif.setSizeThreshold(factorySizeThreshold);
if (factoryRepository != null)
dif.setRepository(new File(factoryRepository));
if (factoryCleaningTracker != null)
dif.setFileCleaningTracker(factoryCleaningTracker);
ServletFileUpload sfu = new ServletFileUpload(dif);
if (sizeMax != NO_LIMIT_SIZE_MAX)
sfu.setSizeMax(sizeMax);
if (fileSizeMax != NO_LIMIT_FILE_SIZE_MAX)
sfu.setFileSizeMax(fileSizeMax);
if (servletHeaderencoding != null)
sfu.setHeaderEncoding(servletHeaderencoding);
if (servletProgressListener != null)
sfu.setProgressListener(servletProgressListener);
return sfu;
}
use of org.apache.commons.fileupload.servlet.ServletFileUpload in project zuul by Netflix.
the class FilterScriptManagerServlet method handlePostBody.
private String handlePostBody(HttpServletRequest request, HttpServletResponse response) throws IOException {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
org.apache.commons.fileupload.FileItemIterator it = null;
try {
it = upload.getItemIterator(request);
while (it.hasNext()) {
FileItemStream stream = it.next();
InputStream input = stream.openStream();
// NOTE: we are going to pull the entire stream into memory
// this will NOT work if we have huge scripts, but we expect these to be measured in KBs, not MBs or larger
byte[] uploadedBytes = getBytesFromInputStream(input);
input.close();
if (uploadedBytes.length == 0) {
setUsageError(400, "ERROR: Body contained no data.", response);
return null;
}
return new String(uploadedBytes);
}
} catch (FileUploadException e) {
throw new IOException(e.getMessage());
}
return null;
}
use of org.apache.commons.fileupload.servlet.ServletFileUpload in project OpenAttestation by OpenAttestation.
the class WLMDataController method uploadManifest.
public ModelAndView uploadManifest(HttpServletRequest req, HttpServletResponse res) {
log.info("WLMDataController.uploadManifest >>");
req.getSession().removeAttribute("manifestValue");
ModelAndView responseView = new ModelAndView(new JSONView());
List<Map<String, String>> manifestValue = new ArrayList<Map<String, String>>();
// Check that we have a file upload request
boolean isMultipart = ServletFileUpload.isMultipartContent(req);
System.out.println(isMultipart);
if (!isMultipart) {
responseView.addObject("result", false);
return responseView;
}
// Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Parse the request
try {
@SuppressWarnings("unchecked") List<FileItem> items = upload.parseRequest(req);
// Process the uploaded items
Iterator<FileItem> iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (!item.isFormField()) {
String[] lines = item.getString().split("\\r?\\n");
for (String values : lines) {
if (values.length() > 2) {
String[] val = values.split(":");
if (val.length == 2) {
Map<String, String> manifest = new HashMap<String, String>();
manifest.put(val[0], val[1]);
manifestValue.add(manifest);
} else {
responseView.addObject("result", false);
return responseView;
}
}
}
}
}
log.info("Uploaded Content :: " + manifestValue.toString());
req.getSession().setAttribute("manifestValue", manifestValue);
/*responseView.addObject("manifestValue",manifestValue);*/
responseView.addObject("result", manifestValue.size() > 0 ? true : false);
} catch (FileUploadException e) {
e.printStackTrace();
responseView.addObject("result", false);
} catch (Exception e) {
e.printStackTrace();
responseView.addObject("result", false);
}
log.info("WLMDataController.uploadManifest <<<");
return responseView;
}
use of org.apache.commons.fileupload.servlet.ServletFileUpload in project OpenMEAP by OpenMEAP.
the class ServletUtils method handleFileUploads.
// TODO: seriously, javadoc the piece handleFileUploads method
/**
* @param modelManager
* @param request
* @param map
* @return
* @throws FileUploadException
*/
public static final Boolean handleFileUploads(Integer maxFileUploadSize, String fileStoragePrefix, HttpServletRequest request, Map<Object, Object> map) throws FileUploadException {
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(4096);
factory.setRepository(new File(fileStoragePrefix));
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(maxFileUploadSize);
List fileItems = upload.parseRequest(request);
for (FileItem item : (List<FileItem>) fileItems) {
// we'll put this in the parameter map,
// assuming the backing that is looking for it
// knows what to expect
String fieldName = item.getFieldName();
Boolean isFormField = item.isFormField();
Long size = item.getSize();
if (isFormField) {
if (size > 0) {
map.put(fieldName, new String[] { item.getString() });
}
} else if (!isFormField) {
map.put(fieldName, item);
}
}
return true;
}
Aggregations