sending files to a php page

Discussion in 'Java' started by Daniel, May 14, 2006.

  1. Daniel

    Daniel Guest

    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...
     
    Daniel, May 14, 2006
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rajive Narain
    Replies:
    0
    Views:
    1,996
    Rajive Narain
    Sep 18, 2009
  2. Srini
    Replies:
    1
    Views:
    220
    Mike Brind
    Oct 29, 2006
  3. Ignoramus6539
    Replies:
    2
    Views:
    236
    Colin McKinnon
    Aug 30, 2006
  4. Replies:
    3
    Views:
    229
    frothpoker
    Sep 4, 2006
  5. M. Strobel
    Replies:
    1
    Views:
    230
    The Natural Philosopher
    Mar 5, 2013
Loading...

Share This Page