if (upload.getFileItems().size() == 0) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "No file was submitted");
return null;
}
DiskFileItem inputFile = upload.getFileItems().values().iterator().next();
InputStream stream = null;
try {
stream = inputFile.getInputStream();
Metadata metadata = new Metadata();
metadata.set(Metadata.CONTENT_TYPE, inputFile.getContentType());
metadata.set(Metadata.RESOURCE_NAME_KEY, inputFile.getFieldName());
long startTime = System.currentTimeMillis();
String content = textExtractionService.parse(stream, metadata);
Map<String, Object> model = new HashMap<String, Object>();
Map<String, Object> properties = new HashMap<String, Object>();
for (String name : metadata.names()) {
properties.put(name, metadata.isMultiValued(name) ? metadata.getValues(name) : metadata.get(name));
}
model.put("metadata", properties);
model.put("content", content);
model.put("file", inputFile);
model.put("extracted", Boolean.TRUE);
model.put("extractionTime", Long.valueOf(System.currentTimeMillis() - startTime));
return new ModelAndView(view, model);
} catch (Exception e) {
logger.error("Error extracting text for uploaded file " + inputFile.getFieldName() + ". Cause: "
+ e.getMessage(), e);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Exception occurred: " + e.getMessage());
} finally {
IOUtils.closeQuietly(stream);
for (DiskFileItem file : upload.getFileItems().values()) {