File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes How to upload a Image using JAVA program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to upload a Image using JAVA program" Watch "How to upload a Image using JAVA program" New topic
Author

How to upload a Image using JAVA program

Ramesh Shanmugam
Ranch Hand

Joined: Sep 13, 2004
Posts: 132
Hi all,

I have a image in my local system. i need to upload it to a remote system.
can any one give me code snippet to upload images to remote system.

Thanks in advance
Ramesh


Ramesh Shanmugam - SCJP 1.5
Manuel Moons
Ranch Hand

Joined: Mar 05, 2002
Posts: 229
Your requirements are a little vague aren't they? Want to use Http, Ftp, something else? Want to use an applet, Browser? Servlet/JSP container? All important factors that need to be filled in before we can give you an answer.

If you want to upload using a Java application (Applet, Webstart, plain app) to a Servlet/JSP engine you can use HttpClient (client side) and FileUpload (server side) in the Apache Jakarta commons project.

For FTP you can also use a Jakarta (Commons Net) package. Or some other FTP library. Of course you will have to have an FTP server somewhere.

If you have a Browser as a client you can use an HTML Form with a file input and the Jakarta FileUpload package on the server side.

Hope this helps you out.
Ramesh Shanmugam
Ranch Hand

Joined: Sep 13, 2004
Posts: 132
Hi,

I am using Browser as a client and i am using HTML Form with a file input.
i donno what i have to use Apache FileUpload software ... can u pls give me out some more tipe..

more over i dont want to use <Input type= "FILE" /> in my form,... i want to pass the File path alone.. using that any software has to upload the file from my local to the remote system..
Manuel Moons
Ranch Hand

Joined: Mar 05, 2002
Posts: 229
If you use the html file-input the upload is done automatically for you. Your browser will create a multipart http request and send the file data to the server. This is the only way of getting the data to the server if you only use a simple http client and a Webserver.

It's not possible for the webserver to access your local filesystem if you only pass a file url (eg. c:\file.txt). The webserver can never get to that file.

Ok, now we have come to that understanding. The only way to go is using <input type="file">. What do we need on the server side? You can not just upload a file to any webserver of course. The server has to know what to do with that content. You will have to parse the multipart request and process all files that have been uploaded in the request. For this you can use FileUpload in the Jakarta commons project.

Using FileUpload

You do have to have a Java Servlet/JSP container in order to use this of course. (eg. Tomcat)

I hope this is clear for you?

Ah yes, if you do not want to manually select the file (you said you do not want to use the <input type="file"> you may try to set the value (using javascript, or on the generated html) and make it invisible using CSS. Just an idea that comes to mind.
[ July 05, 2005: Message edited by: Manuel Moons ]
Ramesh Shanmugam
Ranch Hand

Joined: Sep 13, 2004
Posts: 132
Thanks moons...
Probably can u pls tell me how to set the file name using Javascript and CSS..

the thing is i need to Upload image files.. will it work with Apache File Upload.. do u have any sample Code Snippet to use with FileUpload

Thanks in advance
Manuel Moons
Ranch Hand

Joined: Mar 05, 2002
Posts: 229
The apache project contains all the sample code you need.

Can't help you with the javascript and CSS stuff. Can't be to hard, can it! Search and you will find!
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Moving to the servlet forum.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Ramesh Shanmugam:
Thanks moons...
Probably can u pls tell me how to set the file name using Javascript and CSS..



You can't. The user needs to choose the file.
It would be a huge security hole if websites could do this.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Manuel Moons
Ranch Hand

Joined: Mar 05, 2002
Posts: 229
You are right. It does not work. The value of an <input type="text"> can not be set that way. Hmm, I'm fresh out of ideas then...
 
Consider Paul's rocket mass heater.
 
subject: How to upload a Image using JAVA program
 
Similar Threads
ftp
Garbage Collection
Reading an Image
Override java.io.File to write to memory?
How to browse & upload the image using struts2?