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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "How to upload a file from applet to the webserver ?  " Watch "How to upload a file from applet to the webserver ?  " New topic
Author

How to upload a file from applet to the webserver ?

Amit Hetawal
Ranch Hand

Joined: Apr 12, 2008
Posts: 48
Hello All,
I want to get around the file upload of the html forms on to my web server.
Now since i have a signed applet which is running on the client machines, i was thinking if i can use the applets capability to upload a give hard coded file on to my webserver. I can make use of any servlet/jsp if needed in order to handle the request on the server side. But am not sure where do i start from in order to upload a file from applet to the webserver.
Can someone provide me a jump start on this.

Thanks,
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41572
    
  54
The applet would use the Apache Commons HttpClient library, while the servlet would use Apache Commons FileUpload. This page links to an article on how to use them.


Ping & DNS - my free Android networking tools app
Amit Hetawal
Ranch Hand

Joined: Apr 12, 2008
Posts: 48
Hello,
i tries using the link provided, and it works beautifully when i try it out in my local server with the code as :

PostMethod filePost = new PostMethod("http://localhost:9080/myapp/ProcessFileUpload.jsp");

But when i try it in my dev, which is like an SSO protected server env,
if i use the above code for it as :

PostMethod filePost = new PostMethod("http://devServer/myapp/ProcessFileUpload.jsp");

The code returns me ok, but my jsp is never executed on the server.

Can you please help, how can i get it running. Do i need to send some user password to get it ??


Thanks,
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41572
    
  54
If the server requires a username/password, then yes, you'll need to send one. HttpClient supports various authentication methods.
Amit Hetawal
Ranch Hand

Joined: Apr 12, 2008
Posts: 48
hello Ulf,

Thanks for your help so far, i was able to remove the SSO from the my upload jsp, and so i can see my post methos hitting this jsp on the server. But now i have this problem of contentType =null, i get this output from my jsp whenevr i try to hit it. Any advice what am i doing wrong:

ERROR :
E SRVE0026E: [Servlet Error]-[the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is null]: org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is null


HTTPCLIENT CODE ;
public static void writeToServer(String imageFile) throws IOException {
HttpClient client = new HttpClient();

File f = new File(imageFile);

PostMethod filePost = new PostMethod("http://devserver/temp/ProcessFileUpload.jsp");
Part[] parts = {
new StringPart("param_name", "value"),
new FilePart(f.getName(), f)
};

filePost.setRequestEntity(
new MultipartRequestEntity(parts, filePost.getParams())
);
int status = client.executeMethod(filePost);


FileUplaod.jsp code :

System.out.println("Content Type ="+request.getContentType());
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload fu = new ServletFileUpload (factory);
fu.setSizeMax(10000000);
List fileItems = fu.parseRequest(request);
Iterator itr = fileItems.iterator();

I just get the exception on the parseRequest as contentType is null



Thanks,
Amit Hetawal
Ranch Hand

Joined: Apr 12, 2008
Posts: 48
hello,
Ok am able to figure out my error, of getting header as null.

The code i changed was :

File f = new File(imageFile);

FilePart s = new FilePart(f.getName(), f);
s.setContentType("multipart/form-data");

PostMethod filePost = new PostMethod("http://devsever/temp/ProcessFileUpload.jsp");
Part[] parts = {
s
};


Setting the content type explicitly, for my image file.


Thanks Ulf for all your help.


Regards,
 
wood burning stoves
 
subject: How to upload a file from applet to the webserver ?