• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Trying to emulate HTTP file upload with an applet

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");

try
{
URL url = new URL( "http://www.mywebpage.com/handleapplet.php" );
uc = url.openConnection();
//Post multipart data
uc.setDoOutput(true);
uc.setDoInput(true);
uc.setUseCaches(false);
//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.");
return;
}

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

add(btnPanel);

setSize(250,70);
setVisible(true);
}//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");
try{
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");
osToServer.writeBytes("------------------------12344353--\r\n\r\n");
osToServer.flush();
osToServer.close();
System.out.println("Wrote data to the server");
}
catch (Exception e) {
System.out.println(e);
System.out.println("did not sucessfully connect or write to server");
System.exit(0);
}//end catch

try{
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");
}

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

}//end method postsim

}//end AudioUp.java

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): https://coderanch.com/t/353686/Servlets/java/Uploading-file-server-oreilly-MultiPartRequest
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: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
my overalls have superpowers - they repel people who think fashion is important. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic