0

Can someone please show me/link a simple multipart form data, http post file upload script I can convert to an applet? Looking for someone that who will go easy on me, I've been after this goal for over a month now and just need this. I'm very new to java and have had several small victories on my way to finishing this project but I absolutely need help with this.

I've already got a few signed scripts working, so I've done a lot on my own, but I need one here bad. I'm just so confused.

user1296537
  • 155
  • 1
  • 1
  • 6

1 Answers1

0

The easiest approach to do an http post from your applet is to use Apache HTTP Client. You'll need to add the jar file to your applet's classpath. A simple example:

HttpClient client = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://example.com/test");
MultipartEntity entity = new MultipartEntity();
entity.addPart("file1", new FileBody(new File("filetoUpload.jpg"), "application/jpg"));
post.setEntity(entity);
client.execute(post)
client.getConnectionManager().shutdown();

If for some reason you cannot use HTTP Client you can still do it with java.net.URL but it's a lot more complicated:

Using java.net.URLConnection to fire and handle HTTP requests

Community
  • 1
  • 1
Abdullah Jibaly
  • 53,220
  • 42
  • 124
  • 197
  • Hmm. Do I have to do anything special to add HttpClient to my jdk installation? I don't think it's recognized. And I noticed the above code doesn't need the html form variable for file specified? – user1296537 Apr 27 '12 at 16:29
  • Updated example and linked to reference for how to add jars to your classpath. – Abdullah Jibaly Apr 27 '12 at 16:50
  • will I need other imports other than httpclient and java.io.* ? – user1296537 Apr 27 '12 at 20:48
  • I don't think so, but I hope you're using an IDE such as Eclipse that will automatically organize imports for you. – Abdullah Jibaly Apr 27 '12 at 23:44
  • I was using the import syntax in the java files before compiling them you know 'import java.io.File;' , etc. Is that not ok? – user1296537 Apr 28 '12 at 16:51
  • It is ok, I was asking because most Java programmers never ask about what imports are needed anymore since the IDE handles that for you. You simply add a reference and imports are mostly automatically added. – Abdullah Jibaly Apr 28 '12 at 23:12
  • I have no clue what IDEs are, I'm extremely new to this, only using the jdk. It would be great if I didn't have to worry about import syntax. – user1296537 Apr 29 '12 at 11:54
  • Would definitely like an alternative methinks. the jdk won't let me compile the source without setting the proper -classpath to httpclient, so no manifest. and it looks like multiple classes are being used from this jar so pointing to one directory obviously doesn't work. and btw, do I need any other jars besides for httpclient? – user1296537 Apr 30 '12 at 12:53
  • You don't need any other jar. Just specify the httpclient jar in your applet tag http://docs.oracle.com/javase/7/docs/technotes/guides/jar/jarGuide.html – Abdullah Jibaly Apr 30 '12 at 17:09
  • Actually, just downloaded the latest build and saw this in the README file (you should read it, lots of good info there): HttpClient main module requires Java 5.0 compatible runtime and depends on the following external libraries: * Apache HttpComponents HttpCore * Apache Commons Logging * Apache Commons Codec – Abdullah Jibaly Apr 30 '12 at 17:12
  • *puts face in hands* well thanks for the applet tag bit, I thought you literally meant to add it to the manifest file once I jarred my class, that the jdk still won't let me compile because it can't find the httpclient package no matter how I link to it in the -classpath. And what's that? External libraries? -_- Beginning to think this is beyond me. This java thing hasn't been to noob friendly. Downloaded eclipse btw, added httpclient to my project with my java file source, have no idea how to compile it to class though. – user1296537 Apr 30 '12 at 17:58
  • Thanks for all your help Abdullah, I actually have gotten to the testing the applet stage. I don't know if it's Netbeans or what but my signed applet just isn't working there, so I signed it just using the cmd tool in Windows with the jdk (using 1.6x). I added all 5 of the dependent lib/jars of httpclient to the applet archive syntax, but I'm getting 'no class definition found errors' in the java console when I try to run it though my java file compiled fine. Would you mind doing me a huge favor and try your script out on your end. If you got it working I can see what I'm doing wrong. – user1296537 May 02 '12 at 23:51
  • Sure I'll give it a shot. Where's a good place to publicly upload an image or something to? – Abdullah Jibaly May 02 '12 at 23:57
  • I don't know, a lot of these public upload sites randomize their links after upload. You might not be able to track what you post over. : / – user1296537 May 03 '12 at 00:35
  • This is a nightmare. There seems to be some human error in the jdk's construction executing syntax in some regards. I've spent 5 hours just trying to get it to read from a custom manifest file to be placed in a jar with the classpath of all the libraries/jars I compiled my java file with. I'm getting 'invalid header field' errors with the manifest despite following the example when it's autogenerated (including carriage return) by the creation of a jar. Even the flag instruction with jar in DOS is wrong for the order to provide the manifest file. It seems to only be recognized last in order. – user1296537 May 03 '12 at 16:15
  • Java would be so awesome under a PHP simplicity model btw. I was making web apps in PHP the day I learned it practically, to where 2 years after I was making full blown e-commerce solutions connected to merchant accounts from scratch. Total shame that doesn't interact on the client end like Java other than through a GUI. Not fond of this Java language so far, just for the package/class/dependency hunting like if I were using Ubuntu with no internet connection, no sudo apt-get. – user1296537 May 03 '12 at 16:34
  • Despite my rant (forgive ^_^) I almost have this working. I just haven't used this language enough to know all the quirks - like what it means when the java applet crashes before it even finishes booting. I've followed your instructions to the letter and then some, but I guess I could be failing to execute the applet correctly? I placed all the suggested code inside ex: init() { "all code between the braces" } oh and the java console errors imply I need more than httpclient in the applet tag. I add 4 dependent jars to it but then the applet just crashes as the progress circle loads. – user1296537 May 04 '12 at 21:47