aspose file tools*
The moose likes Applets and the fly likes upload file through applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "upload file through applet" Watch "upload file through applet" New topic
Author

upload file through applet

Nesrin aboud
Ranch Hand

Joined: Feb 08, 2010
Posts: 48
I have a string representing the path of file on client side
this file was created by an applet on client side
and I want to upload it to server side

can I create it directly on server side without depending on client side
please give a sample of code if this is works

or can I upload it inside the applet after creation of the file on client side
and how to do this
can anybody help me in this issue
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18154
    
    8

If you want to upload a file to a server, there must be some code running on the server which is prepared to accept file uploads. This is true not just for applets but for anything which wants to upload a file.

This "code" on the server might be an FTP server or it might be a web application which accepts uploads via HTTP or there might be some other specialized server. So before you write your applet you will have to find out what the server is actually providing to allow for uploads.
Nesrin aboud
Ranch Hand

Joined: Feb 08, 2010
Posts: 48
how can I use a httpclient inside the applet to upload a file ?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
See http://faq.javaranch.com/java/FileUpload for links to information on how to use the HttpClient and FileUpload libraries (you'd use the latter to handle the server part of an HTTP file upload).


Ping & DNS - updated with new look and Ping home screen widget
Nesrin aboud
Ranch Hand

Joined: Feb 08, 2010
Posts: 48
I have used the following code in the applet



and in the ser.do

I wrolte the following


but the input stream is always =-1

can anybody help me in this issue?
why inputStream.read(buffer) always=-1
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
Don't try to work with the stream directly; use the FileUpload library instead.
Nesrin aboud
Ranch Hand

Joined: Feb 08, 2010
Posts: 48
can you please declare the reason
why I can not use it directly?

I tried to use it with submitting to struts action and it failed as I wrote above

but with servlet it succeeded

is there a security issue or what is the problem with it

and how to use FileUpload with it

inside the servlet I wrote the fllowing
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
The FileUpload user guide has full examples of how to use it. If you have problems with the code you posted, tell us in detail what is and is not working.
Nesrin aboud
Ranch Hand

Joined: Feb 08, 2010
Posts: 48
the code is working and the file is transfered
but if several users used the applet at the same time

the transfered streams of bytes interferes with each others and the images appear as upper half from one user and the lower half from another user
what i want is to keep the bytes from each user away from the others
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
That sounds as if your servlet is not thread-safe. Are you using instance variables, by any chance?

You'll also need to make sure that there's no way two files are stored under the same name.
Nesrin aboud
Ranch Hand

Joined: Feb 08, 2010
Posts: 48
all the servlet code I add it above and all variables used are in this part of code
how to make it thread save so
Nesrin aboud
Ranch Hand

Joined: Feb 08, 2010
Posts: 48
I am sure that there is no 2 files with the same name but the problem I think as you said is that the servlet is not thread safe
but I think this will affect the performance
what do you think
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
Ulf Dittmer wrote:You'll also need to make sure that there's no way two files are stored under the same name.

The code above uses the same file name for all files - of course they'll interfere with one another if there are parallel uploads.

Nesrin aboud wrote:but I think this will affect the performance

Correct behavior is more important than performance, no? But the problem is more likely the identical file names.
Nesrin aboud
Ranch Hand

Joined: Feb 08, 2010
Posts: 48
no this was just a test but there is another part of code that generate different names but the problem exists also
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
In that case, post the code that you're actually using.
Nesrin aboud
Ranch Hand

Joined: Feb 08, 2010
Posts: 48
I generate the name by sequence in database so it will never interfere
Nesrin aboud
Ranch Hand

Joined: Feb 08, 2010
Posts: 48
would you please help me making this servlet thread safe
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18154
    
    8

Nesrin aboud wrote:would you please help me making this servlet thread safe

You're asking about the code which you haven't yet posted?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: upload file through applet
 
Similar Threads
sending image file from applet to servlet
Applet upload form
How to upload a file from applet to the webserver ?
can we create folder in client machine using EJB RMI?
Creating File object problem