• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

getParameter, getInputstream, getReader and uploading an image?

 
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok , what I am trying to do is get an image from the form I submit and store it in a database. (don't ask why or you should store it not in a database just a directory)

<input type=file ...> is on my form and when I click the submit button I can use getParameter to get the value of my text based fields , checkbox's ,...etc
But when uploading a file ... It just gives me the name of the file not the contents.

So I need away to get the contents of this upload ,i.e the binary.

So then I tried the function response.getInputStream and I was able to write a simple parser that extracts the image from the html body submitted data and allows me to write it to a database as I wanted.

My problem now is I will have to parse thru all the other fields that I could easily have gotten from using getParameter....

Since the java doc clearly say you can use only one or the other getParameter , getInputstream .

So I am stuck between a rock and a hard place since my parser will probably not be portable for many different webserver if I host it on different sites/computers.

Is their anyway of using getParameter to get my fields like normally and some other function to get the binary/contains of the uploaded file so I can write it into my databases BLOB filed for the images.
Without having to use just getInputStream and write a parser each time to get contents of an uploaded file?

I have been using j2ee 1.5 but I have noticed that j2ee 1.6 has a getPart function. Wondering if that function is the one that will do what I want. I would be willing to upgrade to that if it is true.
Does anybody no for sure?

Also if I can use getParts to do it then can it be used with getParameter at the same time or am I stuck with using one or the other.

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have use something like bellow:

if (ServletFileUpload.isMultipartContent(request)) {
ServletFileUpload servletFileUpload = new ServletFileUpload(
new DiskFileItemFactory());
List multiParts = servletFileUpload.parseRequest(request);
FileItem fileItem = null;
Iterator iterator = multiParts.iterator();
while (iterator.hasNext()) {
try {
fileItem = (FileItem) iterator.next();
if (fileItem.isFormField()) {
if (fileItem.getFieldName().equals("prama")) { // pramais request parameter

if fileItem is not formField then it is file.
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Servlet 3.0 specification comes with built-in support for file upload with @MultipartConfig (and related methods). But you can also use File Upload library from Apache to do the same with containers do not support Servlet 3.0 spec.

@Miku Ranjan : I guess you are talking about the Apache File Upload. Please make sure to use code tags when posting code in the forums.
 
Sam Doder
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, question if I use apaches functions to get the file contents (i.e like the code above) will I still beable to use getParameter for getting other parameters from the form such as text?

And if I use the j2ee api (servlet 3.0) I believe getParts would get me the content but can I still use getParameter with it to get the other parameters like textfields ,...etc

Don't want to run into the problem of not being able to use getParameter.

Also as you have said servlet 3.0 contains a built in function to get it (are you talking about getPart or something different)


 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Prior to Servlets3, the getParameter() and related methods will only work if the request content type is application/x-www-form-urlencoded. Which means the answer is no for any uploads.

Whether this has changed in Servlets3 or not, I can't answer because I haven't had the opportunity to check it out yet.
 
Sam Doder
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok , curious how did people use to code it before the apache library and servlet 3.0 was available?
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both of the Apache commons library and the O'Reilly library have been around for a long long time. Without either of these, we'd be forced to parse the multi-part request ourselves. Not a task for the faint of heart, and not recommended!
 
Sam Doder
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya, I can testify to that because I was starting to parse thru the getInputStream by ----Web.....etc delimiter words ,...etc etc not fun.
Thanks for the code guys I think I will use it or getPart for j2ee 1.6

What is the O'Reilly library one that you are talking about (link please )

And would it be safe to say that these libraries apache/o'reilly have been around since the time the internet had forms that you could upload files on submit of an html page.





 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See the JspFaq for links and more information on uploading.
 
Sam Doder
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok , the apache one works well for uploading images and texted. (all set with that one)

My last question is I have an <input type="file" ... /> tags I want to put a clickable image in it's place that does the same thing but shows the image before submitting the form (like some websites do)

I want to know how I can create a picture I can click on to upload a file /image but when they upload the image how does some websites display the images picture before the form is submitted.
I was thinking this is some client side code like javascript.

I can easily write javascript code that when you click on an image it pops up a upload dialog box. By just makeing the <input type="file" .../> hidden and then writting javascript so onclick of the image it clicks the hidden input file tag. But I am stuck because I cann't seem to get the complete path to their file. So I cann't set the src of the image using javascript because I get c:\fakeroot problem?
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't create a path to the file on the client. You have no access at all to the client file system. For what you are asking, you'd need to upload the file to the server in the background so that you could create a thumbnail and create an image tag to display that.
 
Sam Doder
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So you are saying that all website that have you upload an avatar or picture and display it's thumbnail picture before the submission of the form. Are really uploading the picture to their site before the submission of the form?

And so if you cancel / get out of the page and not submit they still have your picture.
Correct me if I am miss understanding.

Is this the only way or does their exist another way?

If this is the only way I would have to submit the picture before the form. Curious what the best way of going about this. Probably some ajax.
 
Marshal
Posts: 28298
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sam Doder wrote:My last question is I have an <input type="file" ... /> tags I want to put a clickable image in it's place that does the same thing but shows the image before submitting the form (like some websites do)

I want to know how I can create a picture I can click on to upload a file /image but when they upload the image how does some websites display the images picture before the form is submitted.
I was thinking this is some client side code like javascript.



So have you tried looking at the code of those sites to see how they do it? (I know Wordpress uses Flash, for example, but I'm sure there are other ways.)
 
Sam Doder
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya , I cann't really make it completely out at some of these sites since they are using external files for their js functions that I am not able to easily view the complete code.
But it looks to be like I am saying with ajax functions to store the image in a tmp directory and then use ajax to get the image and set it on the page.

All I was really wondering now is if you must submit the picture even before the form is submitted? If that is the case then ajax will work for me and I am sure in flash their is an ajax equivalent functions to do simlar tasks.

I remember when I could get the complete src path of a file thru javascript , windows script hosting , or vbscript on the client machine but those ways would never work well with todays web security enforced in most peoples browsers by default.

Just need somebody to confirm for the last time that you must upload before displaying thumbnail picture.
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The complete path is only available in IE. That's a security breach, so IE9 may have fixed it -- I'm not in a position to test that right now.
 
Grow your own food... or this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic