0

I have a Java applet that needs to upload a file from the user's computer to the server. I am unable to add other libraries (such as com.apache). Is there a low level way of doing so. Currently, I have a php file on the server which contains:

    //Sets the target path for the upload.
    $target_path = "spelling/";

    $_FILES = $_POST;
    var_dump($_FILES);

    move_uploaded_file($_FILES["tmp_name"], $target_path . $_FILES["name"]);
?>

Currently my Java program is sending parameters via POST to this php file. It sends these parameters by POST using the following code:

     try {   
        //Creates a new URL containing the php file that writes files on the ec2.
        url = new URL(WEB_ADDRESS + phpFile);
        //Opens this connection and allows for the connection to output.
        connection = url.openConnection();
        connection.setDoOutput(true);

        //Creates a new streamwriter based on the output stream of the connection.
        OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());

        //Writes the parameters to the php file on the ec2 server.
        wr.write(data);
        wr.flush();

        //Gets the response from the server.
        //Creates a buffered input reader from the input stream of the connection.
        BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line;

        //Loops through and reads the response. Loops until reaches null line.
        while ((line = rd.readLine()) != null) {
            //Prints to console.
            System.out.println(line);
        }

        //Closes reader and writer.
        wr.close();
        rd.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

This works for POST'ing data but when I try to send a file using this method, nothing happens (no response from the server nor is the file uploaded). If anyone has any hints I would be grateful :)

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Bryan Muscedere
  • 572
  • 2
  • 10
  • 23
  • 2
    You'll have to use multipart/form-data to upload files see http://stackoverflow.com/questions/14887681/express-parsing-multipart-form-data-post-in-req-body/14887798#14887798 for an example of how it looks like. – Musa Mar 24 '13 at 03:48

1 Answers1

0

Are you using java.net.URLConnection?

You may want to get some help on this page:

http://www.codejava.net/java-se/networking/upload-files-by-sending-multipart-request-programmatically

Here is the main part:

    boundary = "===" + System.currentTimeMillis() + "===";

    URL url = new URL(requestURL);
    httpConn = (HttpURLConnection) url.openConnection();
    httpConn.setDoOutput(true); // indicates POST method
    httpConn.setDoInput(true);
    httpConn.setRequestProperty("Content-Type",
            "multipart/form-data; boundary=" + boundary);
    outputStream = httpConn.getOutputStream();
    writer = new PrintWriter(new OutputStreamWriter(outputStream, charset),
            true);

But, you will need to have the php script be on the same server where your applet came from.

James Black
  • 41,583
  • 10
  • 86
  • 166