Upload word document - java

I need to extract text from the word document uploaded by the user. I got the code to extract words from a document located on my m/c. But my requirement is to allow users to upload their own document using the upload button & read that document(I dont need to save that document). Can you please suggest me how to go about this? I need to know what all need to happen after clicking the upload button.

When the user uploads their file, grab the associated InputStream and store it to a variable, like inputStream. Then just take the example code, and replace this line:
fs = new POIFSFileSystem(new FileInputStream(filesname));
...with something like:
fs = new POIFSFileSystem(inputStream);
Should be simple enough, assuming that you already have a Servlet in place to handle the upload.
Edit:
Here are the basics of how the servlet could work, assuming that you are using commons-fileupload to parse the upload:
public class UploadServlet extends HttpServlet {
#Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
// 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
List<FileItem> items = upload.parseRequest(request);
//this assumes that the uploaded file is the only thing submitted by the form
//if not you need to iterate the list and find it
FileItem wordFile = items.get(0);
//get a stream that can be used to read the uploaded file
InputStream inputStream = wordFile.getInputStream();
//and the rest you already know...
}
}

Related

Rest Controller: uploading file using HttpServletRequest through Swagger UI

I wonder if it is possible to show the input file to select in my Swagger UI when exposing the API.
I confirm that it is working perfectly when defining the endpoint as following:
public ResponseEntity<String> upload(#RequestPart MultipartFile file)
There is no doubt to show the file chooser when using MultipartFile.
And as I would like to upload big files, I have to use HttpServletRequest instead in order to upload the file as a stream. The endpoint will be then like:
public #ResponseBody Response<String> upload(HttpServletRequest request) {
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload();
// Parse the request
FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName();
InputStream stream = item.openStream();
if (!item.isFormField()) {
String filename = item.getName();
// Process the input stream
OutputStream out = new FileOutputStream(filename);
IOUtils.copy(stream, out);
stream.close();
out.close();
}
}
return new Response<String>(true, "Success", "");
}
The endpoint is working well using a curl command. But unfortunately when using the Swagger UI, I am not able to show the file chooser.
Does anyone faced the same case or knows if Swagger supports this feature?

Writing unit test for file uploader in Junit/Mockito

I am trying to write a unit test for following component to upload file:
#Component("uploader")
public class FileUploader {
public List<FileItem> processFileUploadRequest(HttpServletRequest request) throws FileUploadException {
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletContext servletContext = request.getServletContext();
File repository = (File) servletContext.getAttribute("javax.servlet.context.tempdir");
factory.setRepository(repository);
ServletFileUpload upload = new ServletFileUpload(factory);
return upload.parseRequest(request);
}
}
I have written unit test using junit/mockito like following:
#Test
public void testProcessFileUploadRequestSuccess() throws FileUploadException {
HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
ServletContext servletContext = Mockito.mock(ServletContext.class);
Mockito.when(request.getServletContext()).thenReturn(servletContext);
Mockito.when(servletContext.getAttribute("javax.servlet.context.tempdir")).thenReturn(this.servletTmpDir);
Assert.assertNotNull(fileUploader.processFileUploadRequest(request));
}
I am getting the following error:
org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is null
at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:947)
...
Can anyone please give any clue regarding this? Thank you.
This error ...
the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is null
... is a result of your HttpServletRequest not having a multipart content type.
You can fix this by adding the following line to your test case:
Mockito.when(request.getContentType()).thenReturn("multipart/form-data; boundary=someBoundary");
Ona side note: your question (specifically, this part: #Component("uploader")) suggests that you are using Spring. If so, then perhaps your file upload code could be more easily tested using Spring's MockMvcRequestBuilders#fileUpload(String, Object...) to return a MockMultipartHttpServletRequestBuilder. Something like this:
mockMvc.perform(MockMvcRequestBuilders.fileUpload("/upload")
.file(aFile)
.andExpect(status().is(200))
.andExpect(content().string("..."));

processing multipart/form-data

I'm doing file upload using XMLHttpRequest() in my jsp and when I do request.getContentType() in my controller I'm getting:
multipart/form-data; boundary=---------------------------4664151417711.
Further I'm not getting how to get the file and get the contents of it in my controller. Please anyone help.
Update --
I'm doing this in my jsp.
function fileUpload() {
var url= document.getElementById("urlId").value;
var file= document.getElementById("xslId").files[0];
var formdata = new FormData();
formdata.append("url", url);
formdata.append("file", file);
var xhr = new XMLHttpRequest();
xhr.open("POST","http://localhost:8080/XlsUpload/openSource.htm", true);
xhr.send(formdata);
xhr.onload = function(e) {
};
}
and in my controller--
public void openSource(#ModelAttribute("domTool") DomTool domTool,HttpServletRequest request,HttpServletResponse response){
String type=request.getContentType();
Further I'm struck how to get the contents of the uploaded file and the value of text field i.e.,URL in my controller. The type i'm getting as multipart/form-data
There is an Apache commons solution called commons-fileupload for parsing multipart content. You can find it here.
The most simple example copied from their tutorial looks like this:
#Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = upload.parseRequest(request);
// iterate over items (i.e. list of FileItem) and access
// the content with getInputStream()
}

Retrieve an Image from a POST request

I am trying to send a picture to my java servlet (hosted on amazon ec2) to later transfer it to amazon s3 and wonder how to retrieve the Image from the post request.
Upload Code
The request is sent through iOS RestKit API like this (pic.imageData is a NSData type):
RKParams* params = [RKParams params];
[params setValue:pic.dateTaken forParam:#"dateTaken"];
[params setValue:pic.dateUploaded forParam:#"dateUploaded"];
[params setData:pic.imageData MIMEType:#"image/jpeg" forParam:#"image"];
[RKClient sharedClient].username = deviceID;
[RKClient sharedClient].password = sessionKey;
[RKClient sharedClient].authenticationType = RKRequestAuthenticationTypeHTTPBasic;
uploadPictureRequest = [[RKClient sharedClient] post:kUploadPictureServlet params:params delegate:self];
Parsing Code Stub
This is how I parse the other 2 parameters on the Java servlet:
double dateTaken = Double.parseDouble(req.getParameter("dateTaken"));
double dateUploaded = Double.parseDouble(req.getParameter("dateUploaded"));
Question
The question is: how do I retrieve and parse the image on my server?
Servlet 3.0 has support for reading multipart data. MutlipartConfig support in Servlet 3.0 If a servelt is annotated using #MutlipartConfig annotation, the container is responsible for making the Multipart parts available through
HttpServletRequest.getParts()
HttpServletRequest.getPart("name");
References:
Servlet 3.0 File Upload handing files and params
Servlet 3.0 File upload Example
Something along the lines of this, using Apache Commons FileUpload:
// or #SuppressWarnings("unchecked")
#SuppressWarnings("rawtypes")
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
if (ServletFileUpload.isMultipartContent(request)) {
final FileItemFactory factory = new DiskFileItemFactory();
final ServletFileUpload upload = new ServletFileUpload(factory);
try {
final List items = upload.parseRequest(request);
for (Iterator itr = items.iterator(); itr.hasNext();) {
final FileItem item = (FileItem) itr.next();
if (!item.isFormField()) {
/*
* TODO: (for you)
* 1. Verify that file item is an image type.
* 2. And do whatever you want with it.
*/
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}
}
}
Refer to the FileItem API reference doc to determine what to do next.

YUI Uploader with Java back-end

I am trying to use the (flash based) YUI Uploader with a Java (Spring-based) back-end.
The typical way of uploading files in the Java Servlet world is to set the ENCTYPE='multipart/form-data' on the HTML form requesting the file from the user. With the right server side APIs (i.e. Commons FileUpload), it is possible to get the file on the server.
But I am stymied by how to achieve this with the YUI Uploader. I am able to reach the Java controller, and I am even able to extract the custom post values. But I have no idea how to extract the binary file data out of the request.
Has anyone out had any luck with a YUI uploader with a Java back-end?
To answer my own question, and to make a long story short, this snippet of code did the trick:
#Controller
#RequestMapping("/FileUploadController")
public class FileUploadController {
#RequestMapping(method = RequestMethod.POST)
protected ModelAndView onSubmit(HttpServletRequest request) throws Exception{
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> /* FileItem */ items = upload.parseRequest(request);
for (FileItem fileItem : items) {
if (fileItem.isFormField()) {
// processFormField(fileItem);
} else {
File uploadedFile = new File("/tmp/junk/" + fileItem.getName());
fileItem.write(uploadedFile);
}
}
return new ModelAndView("index");
}
}
This example uses Spring, but you should be able to do exactly the same as long as you have HttpServletRequest object.

Categories