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)?