Win a copy of Getting started with Java on the Raspberry Pi this week in the Raspberry Pi forum!
  • 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
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

How to upload a file from applet to the webserver ?

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Amit Hetawal
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the server requires a username/password, then yes, you'll need to send one. HttpClient supports various authentication methods.
 
Amit Hetawal
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Just let me do the talking. Ahem ... so ... you see ... we have this tiny ad...
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic