Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Trying to emulate HTTP file upload with an applet

Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)?
author and iconoclast
Posts: 24203
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Jaime Lannister
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
CAUTION! Do not touch the blades on your neck propeller while they are active. Tiny ad:
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic