0

I'm trying to create a web service for my existing java project. In my java project, it is able to process a document when entered the file path location and insert it into a Postgresql database. I made a dynamic web project (using Tomcat 6, RESTful web service, and jersey). So far in the web project, it has an "Choose File" button on a localhost browser which allows me to choose a file. Afterwards, I can "Submit" the document; however right now it just uploads the document to a different location on my computer. Would it be possible for me to "Choose File" and then pass the file path location to my java project which will process it and insert it into my database? If so, how will I be able to do so?

Any ideas are much appreciated, thank you for your time!

Elilsabeth

0

3 Answers 3

1

In a Java servlet you need to implement the doPost method. In that method, use the FileUpload API from Apache http://commons.apache.org/fileupload/ to access any uploaded files from the client.

Here is something to get you started:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     DiskFileItemFactory disk = new DiskFileItemFactory();
     disk.setRespository(new File(TEMP_STORAGE));

     ServletFileUpload upload = new ServletFileUpload(disk);
     upload.setFileSizeMax(MAX_FILE_SIZE);
     List<FileItem> files = upload.parseRequest(request);

     //...get actual file with files.get(index).getInputStream() and process them....
     .....
}

But read their documentation before implementing anything

On the client side, have a form with this kind of markup:

<form enctype="multipart/form-data" ...
1

You need to use a java servlet and look into the following library: http://commons.apache.org/fileupload/ Should be straightforward from there.

0

There is no reliable way to simply send the filename from your client to the server and then have the server fetch the file. Instead, you need to upload the entire file (even if the client and server are on the same machine) if you want your software to be more useful than the existing command-line client for Postgres.

If you expect to accept large files, I highly recommend that you use a streaming approach to push the file's contents directly from the request into the database. Otherwise, you run the risk of putting a lot of pressure on your heap space (for instance, if you try to load the whole file into memory) or running into performance and synchronization problems while writing to the disk to buffer the file. Since the JDBC API is capable of streaming data to a database, your best bet is to connect the requests' InputStream directly into the database.

Note that most "file upload" APIs including Apache commons-upload and the Servlet 3.0 file-upload features are rigged to upload files to temporary files on the disk and then delete them after the request has been processed. I don't believe any of the offer streaming capabilities, so you may have to write your own.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.