Strange behavior with the method getUploadedBlobs - java

I've a problem with the methode blobstoreService.getUploadedBlobs(). I've a JSP page in wich one I set an uploader like this :
<formname='form' action='<%= blobstoreService.createUploadUrl("/Edit_Engine") %>' method='POST' enctype='multipart/form-data' >
<input label='...' multiple='false' name='myFile' />
//...and multiple input for text
</form>
and I retrieve this code with my servlet :
java.util.Map<String,BlobKey> blobs = blobstoreService.getUploadedBlobs(req);
BlobKey blobK = blobs.get("myFiles[]"); //I don't know why I need to add the characters 's[]' at the end...
But the behavior is strange. The first time I upload an image, everything works. However, the second time, I send my form without uploading somehting (only text data), and then my java code finds a BlobKey. But this BlobKey seems to be the previous sended data, or a corrupted data.
I mean that not normal, because when I deploy this version on my localhost, if the form uploads no file the method getUploadedBlobs returns an empty HashMap. However, when I deploy on google servers, if the form uploads no file, the method getUploadedBlobs seems to return a HashMap with wrong data.
Could you help me? Or tell me if this behaviro is normal...
Many thanks,
bat

If you're getting a valid BlobKey, then myFiles[] is most likely the name given to the file input field in the form. Is that the case? That seems like an odd name for an input field. Are you using a template library to help generate HTML from the JSP?

Related

How to enter data from excel sheet to mysql database using JSP? JSP + Servlet + MySQL Project

So, basically what I want to do is, on a jsp page give option to browse and select an excel file. Then when the user clicks on Upload button, the data from excel is saved into database table.
I am able to insert into database table without the UI i.e., when there is only servlet and database. In that case, I am doing this:
InputStream fis = new FileInputStream(new File("C:\\Users\\RAJYAWARDHAN\\Desktop\\Book1.xlsx"));
And after this I use fis to one by one enter data into Database table using Apache POI. But when JSP page is included I am using form:
<input type="file" name="file_uploaded" />
So, when not using JSP page, I have the address of file, that is on my machine only. But that's for test purpose. When user will upload, then I will not have the address of the file because file is on users machine. Hence, I cannot use new File("address_of_file") as file is not on my machine(server basically).
Also, I don't want to first upload file from users machine to my machine and then pick it up from that particular path. I just want to import data from the selected file by user and not actually upload and save that file.
What should I do? Please explain in simple terms. Thank You.
Please ask if question is not clear. English is not my first language.
I think what you want to use is getPart method in servlets.
Just like request.getParameter you can use request.getPart("file_uploaded"). It gets file for the given name from front end(jsp/HTML).
This returns Part object which can be converted to InputStream. You don't need to save file to some location in this case.
Do not forget to forget to set enctype= multipart/form-data in your form.

Add binary data to html form for upload

Background: I have to support multiple file uploads in IE7-9. I've found uploadify and FileReader which are both flash based.
Our current file upload allows the user to select a file, type in a description and check some check boxes. That data is all sent to the upload servlet at the same time and the servlet gets the file data and the description and the checkbox values and stores the file on the server and adds an entry into the database.
The problem: uploadify and FileReader both want to send the file directly to the server, I don't have a chance to add a description or set any flags. I've worked with FileReader some now and can intercept the file instead of sending it to the server. What I would like to do is get the binary data and put it into the form, let the user add the description and then submit the form with the binary file data.
I've all ready tried just adding a hidden field to the form but the data didn't seem to come through.
If worse comes to worse I think I could just upload the file and then update the database when the form is submitted, I don't want to do that but I think that would work.
Does anyone know of anyway to add the file data to the form and then to get the servlet to recognize that data as part of the form?
You can pass data along side your upload in Uploadify, just use the formData attribute like this (found here):
<input name='someKey' type='text' value='Some Value'/>
<input type="file" name="file_upload" id="file_upload" />
<script>
$('#file_upload').uploadify({
// Some options
'method' : 'post',
'formData' : { 'someKey' : $('input[name=someKey]').val() }
});
</script>

java - how to get data of a file from a form?

I've created a form and I need the user to enter some info then upload a picture.
So lets say I have something like this:
<form method="post" enctype="multipart/form-data" action="some servlet/filter">
<input type="file" name="logo">
</form>
I need to use java to change that data to a byte[] then to blob so I can insert to a table.
How do I get the data from this form?
A bit of info on this:
I created the page using javascript, then when submitted it will call a java function to handle the data from the form. It seems that when I submit the form the data for the file is not passed over to the servlet.
So far the few methods I've tried have returned null and I'm outta ideas...
Any examples/help is greatly appreciated!
I think the main question I have is where is the file data stored before the java file start working on it? Is a special global variable holding the data or something like that?
You can use the Apache Commons FileUpload library.
The Commons FileUpload package makes it easy to add robust, high-performance, file upload capability to your servlets and web applications.
FileUpload parses HTTP requests which conform to RFC 1867, "Form-based File Upload in HTML". That is, if an HTTP request is submitted using the POST method, and with a content type of "multipart/form-data", then FileUpload can parse that request, and make the results available in a manner easily used by the caller.
If I understood you right, you need something similar to this example:
http://www.servletworld.com/servlet-tutorials/servlet-file-upload-example.html
I used the following tutorial one year ago:
http://balusc.blogspot.com/2009/12/uploading-files-in-servlet-30.html
It looks like it's a lot, but it's actually easy to understand, and it has a lot of good information.

uploading files by url

I need to implement a servlet that uploads files to a server, I realize everyone says it has to be a POST method in regard to uploading files and not with GET method. However is there a way to upload a file and have the parameters of the request show up in the url even if the request is coming from POST method? If not, is there another approach?
Currently my servlet using post method is http://example.com/FileUpload/UploadFile
What I want is somehting like http://example.com/FileUpload/UploadFile?id=125&fileNum=5
Thanks for your input.
Simply POST to
http://example.com/FileUpload/UploadFile?id=125&fileNum=5
instead of
http://example.com/FileUpload/UploadFile
There is no such restriction that you cannot post to an URL having parameters. You can process the post data as you are doing now, plus, you can get the get parameters also.
I think it would not be an elegant solution, but you could use JavaScript to alter the action of the form element before submitting it to include querystring parameters.
The form will be something like:
<form method="POST" id="myForm" onSubmit="submitMyForm(this)>
<input type="text" id="id">
Then you will need JavaScript to change the action element of the form:
function submitMyForm(theForm) {
theForm.action="http://example.com/FileUpload/UploadFile?id=" +
getElementById("id").value;
theForm.submit();
}
Is there some reason you cannot just submit the parameters with post and pull them out on the server side?
Alternatively, if you do a multipart/form-data post you can include multiple parameters along with your file. The parameters are sent as part of the post body, along with the file.
You can send parameters and files in the POST. For example in the html you can have a form with this values, they can be of hidden type.
In the servlet you can get the values in the same way you do using the GET.
It is also better to use the POST method because the user can't change the value in the URL direction bar.

send large text to server using html form hidden value

HI all
I need to send an dynamically generated html to server using html form, html can be bigger size at present it is 1MB
I m sending an dynamical generated html to server using form hidden input field. at server side exception is : too large content..
The dynamically generated html is used to generate pdf and generated pdf will send back to browser in same request of response.
How to handle bigger size html which is generated dynamically.
Please help me out.
Thanks
kumar kasimala.
If you use asp.net you can do like below:
In Web.config file, add
<httpRuntime maxRequestLength="100000" executionTimeout="360"/>
under <system.web>
I googled a bit on your problem and hit upon this page which says that the post size is set by the server in its configuration and can be changed by resetting it - http://forums.sun.com/thread.jspa?threadID=5400480
If you use java:
Use servlet post method to transfer the data to the server
<form method="post" name="sample_form" action="/xxx">
....
</form>
Split it.
Try to split the content to multiple inputs. Your implementation might have problem with that... but that's not too probable
Split the content to multiple requests and send them with AJAX. Collect the responses and be sure to send it in the right order (not all requests at once). Last request should confirm it's the end and load a page returning the pdf

Categories

Resources