File upload through web service and java project to postgresql db - java

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

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

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

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.

Related

java - send a file to client by file URL without download on the server

In my web application I have a link which, when clicked, invokes an external web service to retrieve a download URL for a file.
I need to send back to client the file which is beyond this URL, instead of the download URL retrieved from the web service. If possible, I would also like to do it without having to download the file on my server beforehand.
I've found this question about a similar task, but which used PHP with the readfile() function.
Is there a similar way to do this in Java 8?
If you doesn't even want to handle that file you should answer the request with a redirect (eg HTTP 301 or 302). If you want to handle the file you should read the file in a byte buffer and send it to the client which would make the transfer slower.
Without seeing your implementation so far, this is my best suggest.

How to get server local drive path in web application?

I am developing a jsf web application in which i have store some image files on my local computer lets say D:\images. The server running on this computer only. How can i access my local drive files on my web application.
I tried
<p:graphicImage value="D:\\Temp\tec0178.jpg">
or
`<p:graphicImage value="D:/Temp/tec0178.jpg">`
this not work for me.
If i place the images in my web application
<p:graphicImage value="resources/images/Male.png"/>
its working.
If you need to store it on a physical location on your server where your application is deployed, you could just work with basic I/0 operations in JAVA using File class.
Local drives are directly accessible.
http://docs.oracle.com/javase/tutorial/essential/io/
You could also explore Google Guava API for the same.
A simple example using Google Guava API is:
File imageFile = new File("D:\\images", imgFileName);
FileOutputStream outputStream = new FileOutputStream(newAttachment);
ByteStreams.copy(inputStream, outputStream);
Also, to access images over URL you'll have to add the directory as context. Check out the blog post to access image file as URL:
http://th1rty7.blogspot.in/2009/05/tomcat-is-often-considered-to-be-too.html
This is not exactly a JSF answer, but I like easy ideas: why don't you write a servlet called Images, for example, that receives as a parameter relative routes to your local directory? That servlet would know exactly where to look (you could have d:\Images stored in some properties) and would be as simple as:
<img src="http://yourserver/yourapp/Images?route=someImage.jpg"></img>
would lead to
ImagesServlet.java
...
// assume 'properties' is some way to access your application properties, be it in database,
// or .properties file, or whatever suits you best. In this case, it points to d:\Images
File imageFile = new File(properties.getRoute(), request.getParameter("route"));
InputStream is = new FileInputStream(imageFile);
ByteStreams.copy(inputStream, request.getOutputStream());
It is better to store your images in web application itself. you can call it like
<img src="\images\appimage.jpg">

Uploaded file creation saving for future usage in GWT server side

I am new to GWT, I am facing some issue with file creation.
I am uploading a file from client side and want to store that file
in server side and parse it and create tables in database for the data
present in the file.
When I try to create a file at server side there is some file.io permission issue
App engine is not allowing me to create the file.
Please let me know how can I solve this issue.
I did browse net, didn't find any solutions.
Thanks in Advance,
Pradeep
You cannot store files to GAE as you do normally on a Web Server because you have no direct access to the file system. GAE gives you other ways to that using services.
There are two ways to store files on GAE :
BlobStore
Google Cloud Storage
I suggest to give a try to gwtupload, it has a servlet and a set of FileItemFactory to store uploaded files in BlobStore, FileApi and MemCache.

Is there a reliable way to limit the size of a file upload via java servlet?

I'm running tomcat and Java EE. I am using apache's FileUpload library to handle file uploads. This is working great actually except when a user tries to upload a large file. There is a way to set a file limit using FileUpload but the entire request still goes through and the browser hangs the whole time. Is there a way to block large requests from even going through? Also is there a way to check file size client side so I can cut off most requests before they start?
How about using a Jquery fileupload plugin
https://github.com/blueimp/jQuery-File-Upload
This allows you to limit the size at the client side and prevent upload before it leaves the client.

How to retrieve images stored in file server?

I have a java web service through which I upload images to a file server. I want to access these images from my java web app. How can I make the image files (and eventually other static files) available from this file server?
The only thing I could think of was to use Apache Http server as a proxy to my web app for these images, but that circumvents the security measures of the web app.
UPDATE:
Servlet container: Tomcat
Web app is on separate server from images.
Web service is on same server as images and has direct access to file system.
Both web app and service use spring security for authentication/authorization, I want to continue to use this security framework to for image access.
How are the files stored?
If security is a concern the best option might be to create a Servlet (or something similar) which will load up the image and serve it to the user, once it has checked their credentials.
How you load the image depends on exactly how they're stored, if you can access them via HTTP you can always open up a URLConnection to the file from the Servlet and serve it directly that way (i.e. using the Servlet as a sort of proxy server).
Without more details it's difficult to be specific.
I'm not sure if this will solve your problem, but it sounds like you should set up a context path that will map a URL to the path on your server. This can be done with tomcat's context files.
For a good explanation of the solution, check out a post on How to Program with Java
Sounds similar to Apache Hadoop.
Once image/file is requested, you have to make API call and pull the file out and do one of the following:
Store the temp file to the "temp" directory on web accessible server. You will need, some kind of cleaner/gc running in the background to clean those temp files. This is how Facebook does it with photos.
Instead of storing file on the server check the file type and set HTTP Content-type header to the appropriate file type. Image source will look like this <img src="getPicture.jsp?id=1234" />

Categories

Resources