Java Upload Applet Request
Asked Answered
B

1

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.

Butterfly answered 26/4, 2012 at 21:9 Comment(6)
You'll get more help if you would mark answers as correct for questions you've asked, that's how this site works.Armament
how do I mark answers as correct?Butterfly
Check the check mark next to the correct answer.Armament
I think I checked them all. sorry wasn't aware of the check clickButterfly
No worries, you learn as you go.Armament
you know anything about java file upload appletsButterfly
A
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

Armament answered 27/4, 2012 at 16:17 Comment(17)
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?Butterfly
Updated example and linked to reference for how to add jars to your classpath.Armament
will I need other imports other than httpclient and java.io.* ?Butterfly
I don't think so, but I hope you're using an IDE such as Eclipse that will automatically organize imports for you.Armament
I was using the import syntax in the java files before compiling them you know 'import java.io.File;' , etc. Is that not ok?Butterfly
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.Armament
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.Butterfly
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?Butterfly
You don't need any other jar. Just specify the httpclient jar in your applet tag docs.oracle.com/javase/7/docs/technotes/guides/jar/…Armament
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 CodecArmament
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.Butterfly
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.Butterfly
Sure I'll give it a shot. Where's a good place to publicly upload an image or something to?Armament
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. : /Butterfly
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.Butterfly
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.Butterfly
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 <init> 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.Butterfly

© 2022 - 2024 — McMap. All rights reserved.