Servlets :: Large File Uploads Through Http Service
Apr 7, 2015
I need to built a file upload service which should be memory effective. I should avoid loading the entire file into memory,Since I may have multiple http request which will pile up the Heap memory. Any effective way to upload a large file(For ex:1GB file) using http Streaming. I need to do the file upload on a single http call. Let's consider a scenario where 1 GB file to be uploaded using 512MB Heap memory. Not Sure If practically I can achieve this or not.
I have a requirement where a large file (100 -200MB) is uploaded from the client to a content management system. I am using a servlet with Apache Commons File Upload API. Apache FileUpload has 2 ways of handling files,
1) Non-Streaming 2) Streaming
Currently I use the Non-Streaming approach where the servlet stores the file in a temp location and upload the same into the content management system - This is taking lot of time so I am trying to implement Streaming API.
Content Management API supports streaming in 2 methods,
a) SetContent - Takes the file's ByteArrayOutputStream as input -> This gives OutOfMemoryException because the file being large b) AppendContent - Takes the file's ByteArrayOutputStream as input -> This method can be called multiple times to upload the large file but I dont know how to do this. The Apache File Upload gives InputStream of the file and I need to split that into chuncks and append into the content management system.
How to convert InputStream to 4KB ByteArrayOutputStream so that I can use the AppendContent method in content management API?
I'm having problems with using the h:inputFile tag with JSF 2.2 and Glassfish 4.0 on Eclipse. The file uploads work when the enctype is set to "multipart/form-data" in the h:form tag, but AJAX will not work. When I leave out the encType (defaults to "application/x-www-form-urlencoded") AJAX works but the uploads do not work, and in fact Glassfish crashes and generates the message:
The request content-type is not a multipart/form-data
Likewise I'm having the same problems with the PrimeFaces file upload tag p:fileUpload. In a JSF 2.0 it works correctly, but with JSF 2.2 it's giving the same problems and generates the message above.
When I start up Eclipse it tells me that Mojarra 2.2.0 is installed. Is this the source of the problem, and if so, how do I install a later version of Mojarra?
I am trying to write a webservice class which actually handles multipart requests. T client will try to upload zip or tar.gz files which may be upto 1GB in size.
I dont want to validate the extension i client side.
I want to validate the file extension in server side and discard the request before the file is uploaded.
This resource is about a login page.But when I logout or when I visit some pages like Contacts or Home Page, I wouldn't use HTTPS protocol.At the moment, HTTPS remain in the url even if I declared this protocol only for that resource..This is my (little) web.xml
I am trying to use hidden variable in project.When I launch my project i am able to get the welcome page.But when submit login values i am getting HTTP 404 error- Resource not found error.
So I have a screen I click on. The "webPage" comes back as the requested resource unavailable.
What I don't get is that when I run the server locally on my machine it works just fine. But when this is deployed out to server I get the error. The screen comes up but the data doesn't show up. Where should I look to troubleshoot this?
We have a website used for downloading large files as large as 6gb and sometimes larger. We have all files in FTP server. The websit is JSP/Servlet /Tomcat combination. Below is code sample. I need a solution to increase the download speed. I understand it is bound to network bandwidth but are the steps that we need to take while we have such large files.I read about multipart downloads,gunzip streaming.
//first connect to FTP server and login and keep connection Calendar cal=Calendar.getInstance(); startTime=cal.getTime(); fis=ftp.downloadFile(ftpclient,separator+resourceID); response.setContentType("application/*"); response.setHeader("Content-Disposition", "attachment; filename="" + downloadFileName + "";"); byte[] bytes = new byte[1024];
Its required to create a service to embed widgets on any foreign website(server), using servlets where i'll pass encrypted widgetId and in turn i will get html for displaying it on screen. Also a refresh button with the embedded widget is to be provided so as to fetch updated computed data. The point is that user should copy some html/script code and place it on his website.
I am thinking of following approach.An empty span with id, followed by a js script to make ajax call to the server to get widget HTML.
When does HTTP Session object is created in web application. Suppose I have a website. Home page of website is HTTP page which contains details of company and link to Login page.
Consider below mentioned user journey as scenario:
a. user arrives at home page of website b. user click on Login page c. user fill in login details on login page and click on Submit d. user is successfully authenticated and authorized from back end e. User specific page is shown f. user click on logout link g. user is successfully logged out from website h. user is redirected to home page i. user closes browser
In the above mentioned user journey,
a. at which step does HTTP session starts (means at which steps does HTTP Session object is created ? ) b. at which step does HTTP session ends ?
In case required, assume tech stack to be Java 7, Servlet 2.5, JSP, Tomcat 7, Apache web server (for static web contents).....
I would like to know the details about how a server handles a multipart request. I know that a webserver comprises of a HTTP adapter(which is responsible for receiving http requests and sending http responses) and a container(which is responsible for handling dynamic requests).
So, when a client sends a request, the HTTP adapter receives it. Then transfers the request to the container if the http adapter cannot handle it(jsps, servlets for example).
Suppose, a client sends a multipart request which has an avi file of size upto 100mb. For example, lets assume that the client takes 5 minutes to upload the file.
Lets also assume that my application is only interested in flv files. Is there any way to stop/terminate the multipart request before the file is transferred to the server? So that the client's time will not be wasted?
I think that the HTTP adapter will transfer the request to the container only after receiving the entire request, i.e. the servlet will be called only after the file is transferred to the server from client. Is it right?
If its right then there is no easy way to terminate the multipart request unless the entire file is transferred to the server. Is this right?
I'm trying to learn java by making a login page using java, servlet, javascript, html and mysql. I can login with username and password, I can get all the information from database in the edit page. However, when I edit and click on "Submit" in EditPage.jsp, it gives HTTP Status 404. Same screen appears when I click on "Sign Up" from first page (NewFile.jsp) and click on "Submit" button after filling up user information.
I am posting all my code here but probably important ones are:
EditPage.jsp, Edit.java, EditDetails.java, UpdateUser.java and for Sign Up SignUpPage.jsp and Registration.java.
Firstly here are my error message and ss from my project explorer:
I am running a test servlet on Tomcat and have implemented different behaviours for the doPost and doGet methods. When I access from the browser, only the doGet method gets called ultimately.
The Firefox developer tools show me a GET request from the browser to my Tomcat instance. Do browsers ever call the POST http method? How could I make this happen?
I am generating java script tag and javascript code in servlet and displaying it in each jsp page. i include this in every jsp in my application. I am preparing the following javascript content and diplayin each jsp
<script type="text/javascript"> BOOM.addVar (clientId = SOME universal unique ID ) </script>
the clientid will be uniqueid it gets generated every time.
Here my question is, is there any possibility the clientId will be store in browser cache or third party cache server. if yes how to prevent clientId from cache.
I don't want to prevent the whole jsp file from cache. i just want to prevent only that particular field. so that i can use advantages cache and also prevent particular header field to be cached.
Also can we prevent particular http header attribute from cache.
Is there any kind of way to generate HTTP request within a servlet, dispatch it to the server and get back the answer delivered to the servlet? Or are the servlets meant only to respond to passed requests, not generate them?
(I asked a similar question here: [Code] ..... but no luck)
I'm a new Java user and I'm trying to code a simple login page. In first page (NewFile.jsp) users should enter their username and password and should click on "login", or click on "sign up".
1.) If user enters his username and password correctly, a login page (LoginPage.jsp) appears and says "welcome null" but it should show the name of that user instead of null.
2.) In that login page there is an edit button to edit profile information. When I clicked on it, every information is "null" and when I edit them and click on "Submit" button;
HTTP Status 404 - type Status report message description The requested resource () is not available. GlassFish Server Open Source Edition 3.1.2
that message appears.
3.) If I click on "Sign Up" button at the beginning, a registration jsp (SignUpPage.jsp) appears. After filling up text boxes and clicking on "Submit", same Status 404 screen appears.
I created a mysql database called "loginpage" using xampp. In that database there is a table called "users" and it has un, pass, name, surname, email and degree attributes.
Regarding the lifecycle of servlet , in headfirst servlet i can find :
You normally will NOT override the service() method, so the one from HttpServlet will run. The service() method figures out which HTTP method (GET, POST, etc.) is in the request, and invokes the matching doGet() or doPost() method. The doGet() and doPost() inside HttpServlet don’t do anything, so you have to override one or both. This thread dies (or is put back in a Container-managed pool) when service() completes.
How can I call the doGet method of the subclass from the superclass. i am not getting this .
I have a web service which is returning a string. I want to extend this web service so that it returns an xml based string. To begin with I am able to return xml based strings from a normal POJO class ....
import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; public class logBook { public static void main(String[] args) throws FileNotFoundException
[Code] .....
So far this is the code I have. It will successfully read the contents of the file(which are below in quotations) and write them to the designated file. My issue is totaling the corresponding costs to their services and then writing that to the file. The if and else if clauses above do not work but how is it that I can get them working?
"John; 67.00; Dinner ; Aug 12 2013; Bob; 200.00; Conference; Sep 11 2013; Clara; 450.00; Lodging; Oct 25 2013; Jamie; 450.00; Lodging; Oct 28 2013; Rachel; 67.00; Dinner; Nov 11 2013; Richard; 200.00; Conference; Dec 17 2013; Nick; 67.00; Dinner; Jan 05 2014;"
reading a large json file which is really huge and it contains pair of id and URL in a array as shown below.. i need to get this all id and URL from this file in my java code and need to perform operations . how to read such a big file and store the contents in java code to process it further.
{ "Array": [ { "id": "1test", "URL": "http://servername/test/1test.html" }, { "id": "2test", "URL": "http://servername/test/2test.html" } ....................many id and URL in the same Array which makes the file Huge.. ] }