dazzle
dazzle

Reputation: 243

Upload word document

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.

Upvotes: 0

Views: 2284

Answers (1)

aroth
aroth

Reputation: 54846

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...
    }
}

Upvotes: 1

Related Questions