In my java web application I have to process excel file from user. There are two way to process this first as File second as InputStreams.
I think InputStreams will be a memory consuming thing.
Is there any possible threat if I first save user uploaded file as .xls or .xlsx and then process it?
What are the cons & pros of both approach?
The best way to process web application files is after uploading completely and saving in your server as a file.
Streaming file processing should be avoided because HTTP model is designed to be a Request, Response model. You shouldn't ask the Web Client wait until you finish the file processing.
Best thing to do is upload file to a directory and send the Web Client a upload success message, with possibly a link where the end user can check for the results in the future.
And having a scheduled task to process the files in the uploaded directory and post the results in the results page.
This way web application will not have unnecessary delays and scale-able.
Related
The background:
I have a Java Spring boot application.
I would like to have an API endpoint where the user can request to download a file.
The file is generated by the application and stored in some remote storage (S3 for example), prior to the download request
The customer does not have an access to the remote storage.
He sends a request to the application and as far as he is concerned the file is coming from the application.
The file downloaded can be very big.
The question
Is there a way to have the application be used as some sort of a tunnel?
Meaning - the application gets a request, start downloading the file chunk by chunk, and every downloaded chunk is also returned to the client.
This will create a similar experience to downloading from S3 itself and does not force the application to save the file locally or upload the whole thing into its memory
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.
I'm looking for a way to handle my data while the multipart-file is being transferred. I've tried servlet and spring boot but both can only direct me to the post function after all files are fully uploaded. is there a way to process the data without waiting for the transfer to be done?
thanks
How can we reduce time at the time of uploading a file from jsp to servlet in apche tomcat and put it in queue and start some other thing at the mean time while uploading of file is going on
You can make the call asynchronous using #Asynchronous
The server should be able to handle any (reasonable) number of simultaneous requests: just let the upload happen and do something else!
Now, to do that on the "same" page as the upload is not possible if you are using plain-old HTTP POST with a form. Instead, you'll have to use one of those Flash-based upload tools or maybe there is one in Javascipt you can use that will send the upload some other way (XMLHTTPRequest?). Then you can have the upload run but the page itself is still functional.
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.