sending files to a php page

D

Daniel

Hello all..
I have a java application that writes a neat logfile(text file) when
it crashes. I would like the crash handling part to send the log to a
php-file on my domain. I have managed to get the php part to recieve
files sent via a webform, but I would like to enable the application
to do the sending rather than requesting the user to do it. Truth to
tell I doubt many users would actually send any files.
I include the code I have so far for sending the log, as well as the
php code I have on the recieveing end. Any help and pointers are
appreciated. I know this have been discussed several times before, but
I have not been able to find anything that works.

regards
Daniel

<-- code -->

the readLog() method returns the textfile as a String. If that is a
bad idea I am more than willing to reconsider that.

private void sendLog(){
try{
// Open the connection and prepare to POST
java.net.URL u= new
java.net.URL("http://localhost/dropper/dropper.php");

java.net.HttpURLConnection uc
=(java.net.HttpURLConnection) u.openConnection();

uc.setDoOutput(true);
uc.setDoInput(true);
uc.setAllowUserInteraction(false);


uc.setRequestProperty("Content-Type","multipart/form-data");

uc.setRequestProperty("Connection",
"Keep-Alive");
uc.setRequestProperty("HTTP_REFERER",
"MediaOrganizer");
uc.setRequestProperty("Content-Type",
"multipart/form-data; boundary=****4353");



java.io.DataOutputStream dstream = new
java.io.DataOutputStream(uc.getOutputStream());

// The POST line
java.io.File file= new
java.io.File("execution.log");

dstream.writeBytes("Content-Disposition:
form-data; name=\"userfile\"; filename=\""+file.getAbsolutePath()
+"\"Content-Type: text/plain\r\n");

dstream.writeBytes(readLog());
dstream.writeBytes("****4353--\r\n\r\n");
dstream.flush();
dstream.close();

// Read Response
try{
int i = uc.getHeaderFields().size()-1;
while(i>-1){
try{

System.out.print(uc.getHeaderFieldKey(i));
System.out.print("=");

System.out.println(uc.getHeaderField(i));
}catch(Exception e){
}
i--;
}
}catch(Exception e){
e.printStackTrace();
}



java.io.InputStream in =
uc.getInputStream();
int x;
int y=0;
while ( y<1000)
{
x = in.read();
System.out.write(x);
if(x==-1){
break;
}
y++;
}
in.close();


}catch(Exception e){
e.printStackTrace();
}
}
}
System.exit( -1);
}

}

and the php code:

<?php

echo('MO ');echo($_FILES['userfile']['error'].' ');
echo($_FILES['userfile']['tmp_name']);
$uploadfile = $uploaddir . basename('MO_'.date('Y-m-d
H:i:s').'.log');
if (move_uploaded_file($_FILES['userfile']['tmp_name'],
$uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "File not recieved ";
}

print_r($_FILES);

?>


which obvisously contains quite a bit of debugging information and
will be cleaned up before I use it seriously...

when I post a file via a webbrowser it reciees the file happily. via
my java-app I get "File not recieved" every time...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top