Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > sending files to a php page

Reply
Thread Tools

sending files to a php page

 
 
Daniel
Guest
Posts: n/a
 
      05-14-2006
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...

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: getting perl and php to talk to each other, was Re: randomlysorting files in php M. Strobel Perl Misc 1 03-05-2013 07:20 PM
PHP Training Institute In Delhi, Live Projects on PHP. Short TermPHP Courses, PHP Scripts, PHP Training with Live Projects. Rajive Narain Java 0 09-18-2009 10:47 AM
Sending files from Servlet/JSP page to ASP Page Srini ASP General 1 10-29-2006 04:49 PM
Writing a PHP and Javascript generated page to a PHP variable... jonnyblazed@gmail.com Javascript 3 09-04-2006 12:45 PM
PHP/Perl/Unix Virus: delete config.php files asap Ignoramus6539 Perl Misc 2 08-30-2006 08:22 PM



Advertisments