File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Applets and the fly likes Trying to emulate HTTP file upload with an applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Badass: Making Users Awesome this week in the Game Development forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Trying to emulate HTTP file upload with an applet" Watch "Trying to emulate HTTP file upload with an applet" New topic

Trying to emulate HTTP file upload with an applet

Jaime Lannister

Joined: Mar 30, 2005
Posts: 18
I am trying to emulate the upload of a file, using the http POST operation (like that used in a form) with an applet. More specifically, I am trying to make a PHP script on the other end *think* I have uploaded a file when all I've really done is send the right series of http headers plus some raw data.

My reasons for wanting to do things in this particular way as opposed to using far easier ways requires a long winded explanation, but according to various threads I've read here and there, it should be possible to do it.

My problem is that I seem to be *almost* succeeding in the sense of establishing a connection both ways with the server, starting the PHP code running, and getting responses back, but the PHP does not recognize anything
coming in that looks like a file to it, so it simply prints out a "No file uploaded" message that I built into it.

I am currently experimenting with the following code, which displays a button in the applet that, when you push it, activates a method that simply sends out the correct http headers followed by a single line of text (which is supposed to be what is contained in the imaginary file) and finally a closing line. This is just for testing purposes, trying to see if I can get it to work.

public class AudioUptest1 extends Applet{

boolean stopCapture = false;
AudioFormat audioFormat;
TargetDataLine targetDataLine;
AudioInputStream audioInputStream;
SourceDataLine sourceDataLine;
DataOutputStream osToServer;
DataInputStream isFromServer;
URLConnection uc;
final JButton captureBtn = new JButton("Capture");
final JPanel btnPanel = new JPanel();

public void init(){
System.out.println("Started the applet");

URL url = new URL( "" );
uc = url.openConnection();
//Post multipart data
//set request headers
uc.setRequestProperty("Connection", "Keep-Alive");
uc.setRequestProperty("HTTP_REFERER", "http://applet.getcodebase");
uc.setRequestProperty("Content-Type", "multipart/form-data; boundary=------------------------12344353");
osToServer = new DataOutputStream(uc.getOutputStream());
isFromServer = new DataInputStream(uc.getInputStream());
catch(IOException e)
System.out.println ("Error etc. etc.");

//Start of GUI stuff
//Register listeners
new ActionListener(){
public void actionPerformed(
ActionEvent e){
//Postsim method sends simulated POST to PHP script on server.
}//end actionPerformed
}//end ActionListener
);//end addActionListener()


}//end of GUI stuff, constructor.

byte tempOutBuffer[] = new byte[100];
byte tempInBuffer[] = new byte[100];

private void postsim(){
System.out.println("Got to the postsim method");
osToServer.writeBytes("------------------------12344353\r\nContent-Disposition: form-data; name=\"testfile\"; filename=\"C:testfile.txt\"\r\nContent-Type: text/plain\r\n\r\n");
osToServer.writeBytes("This is a test file");
System.out.println("Wrote data to the server");
catch (Exception e) {
System.out.println("did not sucessfully connect or write to server");
}//end catch

String fieldName="",fieldValue="";
System.out.println ("Into the fieldValue loop!");
for ( int i=0; i<40; i++ )
fieldName = uc.getHeaderFieldKey(i);
fieldValue = uc.getHeaderField(fieldName);
System.out.println (fieldValue);
}catch(Exception e){
System.out.println ("Didn't get any headers");

String sIn = isFromServer.readLine();
for ( int j=0; j<40; j++ ){
if (sIn!=null){
sIn = isFromServer.readLine();
}catch(Exception e){

}//end method postsim


I have looked at examples of HTTP headers being used in Java for file upload, plus sites explaining them in gory detail (including the following thread on this site):
and can find nothing obvious wrong with mine.

I should also mention that the PHP code works just fine with real files uploaded from a form in a web page.

Also the applet is in a signed jar file and is trying to upload to PHP script on the same directory that it came from.

Also I not getting any errors at all from java or the server.

Some specific questions:
1)Can the random number at the end of the "border"
(the one preceded by a few dozen dashes) be any number/letter combo? Or is there some rule for it? I've noticed that most people seem to use a "d" as the second letter, but not all, and have seen some threads where people explain that it can be any random number sequence.

2) Is there some security reason why even a signed applet will not upload a file from your machine (but note that I'm not *really* uploading a file, just sending headers, and also I'm not getting any security violation messages).

3)Do I have to identify the browser I am sending from (even though the browser itself is not doing the sending)?
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24189

Barring the usual unsigned applet security restriction that you can only connect to the server the applet was served from, there's nothing to stop you from doing this. But the problem is that the HTTP file upload protocol is ugly and devilishly hard to implement. Without looking at your code, I can assure you that it's exceedingly likely that you've got some small detail wrong.

Instead of doing it yourself, why not use the well-known O'Reilly library? See here to get a copy. Use this from your applet, and you'll be uploading data in no time.

[Jess in Action][AskingGoodQuestions]
Jaime Lannister

Joined: Mar 30, 2005
Posts: 18
Thanks. Your reply is encouraging - I was beginning to think its impossible. Anyway I'm going to check out O'Reilly.

Another thing that I thought of trying was to get something that would intercept and save the info being sent from a real html upload form, and from my applet, and then compare them in detail to find out what the difference is. Would you happen to know of any way to do this? I've heard that packet sniffers can do something like this, but don't know anything about them.
I agree. Here's the link:
subject: Trying to emulate HTTP file upload with an applet