• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

upload file through applet

 
Nesrin aboud
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21117
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can I use a httpclient inside the applet to upload a file ?
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Nesrin aboud
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't try to work with the stream directly; use the FileUpload library instead.
 
Nesrin aboud
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no this was just a test but there is another part of code that generate different names but the problem exists also
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case, post the code that you're actually using.
 
Nesrin aboud
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I generate the name by sequence in database so it will never interfere
 
Nesrin aboud
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
would you please help me making this servlet thread safe
 
Paul Clapham
Sheriff
Posts: 21117
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic