use of org.apache.commons.fileupload.disk.DiskFileItemFactory in project OpenRefine by OpenRefine.
the class ImportingUtilities method retrieveContentFromPostRequest.
public static void retrieveContentFromPostRequest(HttpServletRequest request, Properties parameters, File rawDataDir, ObjectNode retrievalRecord, final Progress progress) throws IOException, FileUploadException {
ArrayNode fileRecords = ParsingUtilities.mapper.createArrayNode();
JSONUtilities.safePut(retrievalRecord, "files", fileRecords);
int clipboardCount = 0;
int uploadCount = 0;
int downloadCount = 0;
int archiveCount = 0;
// This tracks the total progress, which involves uploading data from the client
// as well as downloading data from URLs.
final SavingUpdate update = new SavingUpdate() {
@Override
public void savedMore() {
progress.setProgress(null, calculateProgressPercent(totalExpectedSize, totalRetrievedSize));
}
@Override
public boolean isCanceled() {
return progress.isCanceled();
}
};
DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(fileItemFactory);
upload.setProgressListener(new ProgressListener() {
boolean setContentLength = false;
long lastBytesRead = 0;
@Override
public void update(long bytesRead, long contentLength, int itemCount) {
if (!setContentLength) {
// Only try to set the content length if we really know it.
if (contentLength >= 0) {
update.totalExpectedSize += contentLength;
setContentLength = true;
}
}
if (setContentLength) {
update.totalRetrievedSize += (bytesRead - lastBytesRead);
lastBytesRead = bytesRead;
update.savedMore();
}
}
});
List<FileItem> tempFiles = (List<FileItem>) upload.parseRequest(request);
progress.setProgress("Uploading data ...", -1);
parts: for (FileItem fileItem : tempFiles) {
if (progress.isCanceled()) {
break;
}
InputStream stream = fileItem.getInputStream();
String name = fileItem.getFieldName().toLowerCase();
if (fileItem.isFormField()) {
if (name.equals("clipboard")) {
String encoding = request.getCharacterEncoding();
if (encoding == null) {
encoding = "UTF-8";
}
File file = allocateFile(rawDataDir, "clipboard.txt");
ObjectNode fileRecord = ParsingUtilities.mapper.createObjectNode();
JSONUtilities.safePut(fileRecord, "origin", "clipboard");
JSONUtilities.safePut(fileRecord, "declaredEncoding", encoding);
JSONUtilities.safePut(fileRecord, "declaredMimeType", (String) null);
JSONUtilities.safePut(fileRecord, "format", "text");
JSONUtilities.safePut(fileRecord, "fileName", "(clipboard)");
JSONUtilities.safePut(fileRecord, "location", getRelativePath(file, rawDataDir));
progress.setProgress("Uploading pasted clipboard text", calculateProgressPercent(update.totalExpectedSize, update.totalRetrievedSize));
JSONUtilities.safePut(fileRecord, "size", saveStreamToFile(stream, file, null));
JSONUtilities.append(fileRecords, fileRecord);
clipboardCount++;
} else if (name.equals("download")) {
String urlString = Streams.asString(stream);
URL url = new URL(urlString);
ObjectNode fileRecord = ParsingUtilities.mapper.createObjectNode();
JSONUtilities.safePut(fileRecord, "origin", "download");
JSONUtilities.safePut(fileRecord, "url", urlString);
for (UrlRewriter rewriter : ImportingManager.urlRewriters) {
Result result = rewriter.rewrite(urlString);
if (result != null) {
urlString = result.rewrittenUrl;
url = new URL(urlString);
JSONUtilities.safePut(fileRecord, "url", urlString);
JSONUtilities.safePut(fileRecord, "format", result.format);
if (!result.download) {
downloadCount++;
JSONUtilities.append(fileRecords, fileRecord);
continue parts;
}
}
}
if ("http".equals(url.getProtocol()) || "https".equals(url.getProtocol())) {
final URL lastUrl = url;
final HttpClientResponseHandler<String> responseHandler = new HttpClientResponseHandler<String>() {
@Override
public String handleResponse(final ClassicHttpResponse response) throws IOException {
final int status = response.getCode();
if (status >= HttpStatus.SC_SUCCESS && status < HttpStatus.SC_REDIRECTION) {
final HttpEntity entity = response.getEntity();
if (entity == null) {
throw new IOException("No content found in " + lastUrl.toExternalForm());
}
try {
InputStream stream2 = entity.getContent();
String mimeType = null;
String charset = null;
ContentType contentType = ContentType.parse(entity.getContentType());
if (contentType != null) {
mimeType = contentType.getMimeType();
Charset cs = contentType.getCharset();
if (cs != null) {
charset = cs.toString();
}
}
JSONUtilities.safePut(fileRecord, "declaredMimeType", mimeType);
JSONUtilities.safePut(fileRecord, "declaredEncoding", charset);
if (saveStream(stream2, lastUrl, rawDataDir, progress, update, fileRecord, fileRecords, entity.getContentLength())) {
// signal to increment archive count
return "saved";
}
} catch (final IOException ex) {
throw new ClientProtocolException(ex);
}
return null;
} else {
// String errorBody = EntityUtils.toString(response.getEntity());
throw new ClientProtocolException(String.format("HTTP error %d : %s for URL %s", status, response.getReasonPhrase(), lastUrl.toExternalForm()));
}
}
};
HttpClient httpClient = new HttpClient();
if (httpClient.getResponse(urlString, null, responseHandler) != null) {
archiveCount++;
}
;
downloadCount++;
} else {
// Fallback handling for non HTTP connections (only FTP?)
URLConnection urlConnection = url.openConnection();
urlConnection.setConnectTimeout(5000);
urlConnection.connect();
InputStream stream2 = urlConnection.getInputStream();
JSONUtilities.safePut(fileRecord, "declaredEncoding", urlConnection.getContentEncoding());
JSONUtilities.safePut(fileRecord, "declaredMimeType", urlConnection.getContentType());
try {
if (saveStream(stream2, url, rawDataDir, progress, update, fileRecord, fileRecords, urlConnection.getContentLength())) {
archiveCount++;
}
downloadCount++;
} finally {
stream2.close();
}
}
} else {
String value = Streams.asString(stream);
parameters.put(name, value);
// TODO: We really want to store this on the request so it's available for everyone
// request.getParameterMap().put(name, value);
}
} else {
// is file content
String fileName = fileItem.getName();
if (fileName.length() > 0) {
long fileSize = fileItem.getSize();
File file = allocateFile(rawDataDir, fileName);
ObjectNode fileRecord = ParsingUtilities.mapper.createObjectNode();
JSONUtilities.safePut(fileRecord, "origin", "upload");
JSONUtilities.safePut(fileRecord, "declaredEncoding", request.getCharacterEncoding());
JSONUtilities.safePut(fileRecord, "declaredMimeType", fileItem.getContentType());
JSONUtilities.safePut(fileRecord, "fileName", fileName);
JSONUtilities.safePut(fileRecord, "location", getRelativePath(file, rawDataDir));
progress.setProgress("Saving file " + fileName + " locally (" + formatBytes(fileSize) + " bytes)", calculateProgressPercent(update.totalExpectedSize, update.totalRetrievedSize));
JSONUtilities.safePut(fileRecord, "size", saveStreamToFile(stream, file, null));
// TODO: This needs to be refactored to be able to test import from archives
if (postProcessRetrievedFile(rawDataDir, file, fileRecord, fileRecords, progress)) {
archiveCount++;
}
uploadCount++;
}
}
stream.close();
}
// Delete all temp files.
for (FileItem fileItem : tempFiles) {
fileItem.delete();
}
JSONUtilities.safePut(retrievalRecord, "uploadCount", uploadCount);
JSONUtilities.safePut(retrievalRecord, "downloadCount", downloadCount);
JSONUtilities.safePut(retrievalRecord, "clipboardCount", clipboardCount);
JSONUtilities.safePut(retrievalRecord, "archiveCount", archiveCount);
}
use of org.apache.commons.fileupload.disk.DiskFileItemFactory in project wcomponents by BorderTech.
the class ServletUtil method extractParameterMap.
/**
* Extract the parameters and file items allowing for multi part form fields.
*
* @param request the request being processed
* @param parameters the map to store non-file request parameters in.
* @param files the map to store the uploaded file parameters in.
*/
public static void extractParameterMap(final HttpServletRequest request, final Map<String, String[]> parameters, final Map<String, FileItem[]> files) {
if (isMultipart(request)) {
ServletFileUpload upload = new ServletFileUpload();
upload.setFileItemFactory(new DiskFileItemFactory());
try {
List fileItems = upload.parseRequest(request);
uploadFileItems(fileItems, parameters, files);
} catch (FileUploadException ex) {
throw new SystemException(ex);
}
// Include Query String Parameters (only if parameters were not included in the form fields)
for (Object entry : request.getParameterMap().entrySet()) {
Map.Entry<String, String[]> param = (Map.Entry<String, String[]>) entry;
if (!parameters.containsKey(param.getKey())) {
parameters.put(param.getKey(), param.getValue());
}
}
} else {
parameters.putAll(request.getParameterMap());
}
}
use of org.apache.commons.fileupload.disk.DiskFileItemFactory in project tech by ffyyhh995511.
the class FileUpLoadUtil method fileUpload.
public static List<UpLoadFileInfo> fileUpload(HttpServletRequest request) throws Exception {
// 图片上传记录信息
List<UpLoadFileInfo> info = new ArrayList<UpLoadFileInfo>();
// 设置编码
request.setCharacterEncoding("utf-8");
// 获得磁盘文件条目工厂
DiskFileItemFactory factory = new DiskFileItemFactory();
// 获取文件需要上传到的路径
// String path = request.getRealPath("/upload");
String path = request.getSession().getServletContext().getRealPath("/upload");
// 如果没以下两行设置的话,上传大的 文件 会占用 很多内存,
// 设置暂时存放的 存储室 , 这个存储室,可以和 最终存储文件 的目录不同
/**
* 原理 它是先存到 暂时存储室,然后在真正写到 对应目录的硬盘上, 按理来说 当上传一个文件时,其实是上传了两份,第一个是以 .tem
* 格式的 然后再将其真正写到 对应目录的硬盘上
*/
factory.setRepository(new File(path));
// 设置 缓存的大小,当上传文件的容量超过该缓存时,直接放到 暂时存储室
factory.setSizeThreshold(1024 * 1024);
// 高水平的API文件上传处理
ServletFileUpload upload = new ServletFileUpload(factory);
try {
// 可以上传多个文件
List<FileItem> list = (List<FileItem>) upload.parseRequest(request);
for (FileItem item : list) {
// 获取不是表单字符串的字段
if (!item.isFormField()) {
UpLoadFileInfo file = new UpLoadFileInfo();
/**
* 以下三步,主要获取 上传文件的名字
*/
// 获取路径名
String value = item.getName();
// 索引到最后一个反斜杠
int start = value.lastIndexOf("\\");
// 截取 上传文件的 字符串名字,加1是 去掉反斜杠,
String fileName = value.substring(start + 1);
// 真正写到磁盘上
// 它抛出的异常 用exception 捕捉
// item.write( new File(path,filename) );//第三方提供的
// 重新定义文件名
String uuid = UUID.randomUUID().toString().replace("-", "");
String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
StringBuffer saveFilePath = new StringBuffer(uuid + "." + suffix);
// 手动写的
OutputStream out = new FileOutputStream(new File(path, saveFilePath.toString()));
InputStream in = item.getInputStream();
int length = 0;
byte[] buf = new byte[1024];
logger.info("获取上传文件的总共的容量:" + item.getSize());
// in.read(buf) 每次读到的数据存放在 buf 数组中
while ((length = in.read(buf)) != -1) {
// 在 buf 数组中 取出数据 写到 (输出流)磁盘上
out.write(buf, 0, length);
}
in.close();
out.close();
// 记录文件信息
file.setSaveFileName(saveFilePath.toString());
file.setReadFileName(fileName);
file.setReadFileSize(item.getSize());
file.setStatus(true);
file.setSaveFilePath(path + File.separator + saveFilePath.toString());
info.add(file);
}
}
} catch (Exception e) {
logger.error(e);
}
return info;
}
use of org.apache.commons.fileupload.disk.DiskFileItemFactory in project OpenClinica by OpenClinica.
the class FileUploadHelper method getFiles.
@SuppressWarnings("unchecked")
private List<File> getFiles(HttpServletRequest request, ServletContext context, String dirToSaveUploadedFileIn) {
List<File> files = new ArrayList<File>();
// FileCleaningTracker fileCleaningTracker =
// FileCleanerCleanup.getFileCleaningTracker(context);
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setFileSizeMax(getFileProperties().getFileSizeMax());
try {
// Parse the request
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()) {
request.setAttribute(item.getFieldName(), item.getString());
// DO NOTHING , THIS SHOULD NOT BE Handled here
} else {
getFileProperties().isValidExtension(item.getName());
files.add(processUploadedFile(item, dirToSaveUploadedFileIn));
}
}
return files;
} catch (FileSizeLimitExceededException slee) {
throw new OpenClinicaSystemException("exceeds_permitted_file_size", new Object[] { String.valueOf(getFileProperties().getFileSizeMaxInMb()) }, slee.getMessage());
} catch (FileUploadException fue) {
throw new OpenClinicaSystemException("file_upload_error_occured", new Object[] { fue.getMessage() }, fue.getMessage());
}
}
use of org.apache.commons.fileupload.disk.DiskFileItemFactory in project OpenClinica by OpenClinica.
the class CreateXformCRFVersionServlet method processRequest.
@Override
protected void processRequest() throws Exception {
CrfDao crfDao = (CrfDao) SpringServletAccess.getApplicationContext(context).getBean("crfDao");
CrfVersionDao crfVersionDao = (CrfVersionDao) SpringServletAccess.getApplicationContext(context).getBean("crfVersionDao");
Locale locale = LocaleResolver.getLocale(request);
ResourceBundleProvider.updateLocale(locale);
resword = ResourceBundleProvider.getWordsBundle(locale);
// Retrieve submission data from multipart request
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> items = upload.parseRequest(request);
String submittedCrfName = retrieveFormFieldValue(items, "crfName");
String submittedCrfVersionName = retrieveFormFieldValue(items, "versionName");
String submittedCrfVersionDescription = retrieveFormFieldValue(items, "versionDescription");
String submittedRevisionNotes = retrieveFormFieldValue(items, "revisionNotes");
String submittedXformText = retrieveFormFieldValue(items, "xformText");
CRFVersionBean version = (CRFVersionBean) session.getAttribute("version");
logger.debug("Found original CRF ID for new CRF Version:" + version.getCrfId());
// Create container for holding validation errors
DataBinder dataBinder = new DataBinder(new CrfVersion());
Errors errors = dataBinder.getBindingResult();
// Validate all upload form fields were populated
validateFormFields(errors, version, submittedCrfName, submittedCrfVersionName, submittedCrfVersionDescription, submittedRevisionNotes, submittedXformText);
if (!errors.hasErrors()) {
// Parse instance and xform
XformParser parser = (XformParser) SpringServletAccess.getApplicationContext(context).getBean("xformParser");
XformContainer container = parseInstance(submittedXformText);
Html html = parser.unMarshall(submittedXformText);
// Save meta-data in database
XformMetaDataService xformService = (XformMetaDataService) SpringServletAccess.getApplicationContext(context).getBean("xformMetaDataService");
try {
xformService.createCRFMetaData(version, container, currentStudy, ub, html, submittedCrfName, submittedCrfVersionName, submittedCrfVersionDescription, submittedRevisionNotes, submittedXformText, items, errors);
} catch (RuntimeException e) {
logger.error("Error encountered while saving CRF: " + e.getMessage());
logger.error(ExceptionUtils.getStackTrace(e));
// and should be allow to crash the page for now
if (!errors.hasErrors())
throw e;
}
}
// Save errors to request so they can be displayed to the user
if (errors.hasErrors()) {
request.setAttribute("errorList", errors.getAllErrors());
logger.debug("Found at least one error. CRF data not saved.");
} else {
logger.debug("Didn't find any errors. CRF data saved.");
// Save any media files uploaded with xform
CrfBean crf = (submittedCrfName == null || submittedCrfName.equals("")) ? crfDao.findByCrfId(version.getCrfId()) : crfDao.findByName(submittedCrfName);
CrfVersion newVersion = crfVersionDao.findByNameCrfId(submittedCrfVersionName, crf.getCrfId());
saveAttachedMedia(items, crf, newVersion);
}
forwardPage(Page.CREATE_XFORM_CRF_VERSION_SERVLET);
}
Aggregations