Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android, http: How to upload a file to a site hosted by a shared server?

I bought myself a website where it is being hosted on a linux server which shares one IP address using cpanel. The problem that is, now, i want to use this code to upload a file to my site. Everytime i use the site address like www.site, i'll get an exception saying that the URL is malformed. When i use the ip address (since this is a shared server), i can't find my php code since i don't know how to link to my address.

Does anyone... anyone know how to link me to my site, just so i can upload an xml file?? Really need help here.......

Any help would be really appreciated as i have no knowledge in networking stuff.

    HttpURLConnection connection = null; 
    DataOutputStream outputStream = null;
    DataInputStream inputStream = null;
    String pathToOurFile = "/data/data/test.send/testsend.txt";
    String urlServer = "http://www.site.com/filefortransfer.php";
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary =  "*****";

    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1*1024*1024;

    try
    {
    FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile) );

    URL url = new URL(urlServer);
    connection = (HttpURLConnection) url.openConnection();

    // Allow Inputs & Outputs
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setUseCaches(false);

    // Enable POST method
    connection.setRequestMethod("POST");

    connection.setRequestProperty("Connection", "Keep-Alive");
    connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

    outputStream = new DataOutputStream( connection.getOutputStream() );
    outputStream.writeBytes(twoHyphens + boundary + lineEnd);
    outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd);
    outputStream.writeBytes(lineEnd);

    bytesAvailable = fileInputStream.available();
    bufferSize = Math.min(bytesAvailable, maxBufferSize);
    buffer = new byte[bufferSize];

    // Read file
    bytesRead = fileInputStream.read(buffer, 0, bufferSize);

    while (bytesRead > 0)
    {
    outputStream.write(buffer, 0, bufferSize);
    bytesAvailable = fileInputStream.available();
    bufferSize = Math.min(bytesAvailable, maxBufferSize);
    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
    }

    outputStream.writeBytes(lineEnd);
    outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

    // Responses from the server (code and message)
    String serverResponseCode = Integer.toString(connection.getResponseCode());
    String serverResponseMessage = connection.getResponseMessage();

    fileInputStream.close();
    outputStream.flush();
    outputStream.close();

    }
    catch (Exception ex)
    {
        Log.e("MediaPlayer", "error: " + ex.getMessage(), ex);
    }
    }
like image 739
Cid Avatar asked Nov 15 '22 04:11

Cid


1 Answers

On Android it's preferable for more complex HTTP networking to use HttpClient class.

Here is an example of multipart file upload: http://rapidandroid.org/wiki/HttpUpload

like image 82
Peter Knego Avatar answered Dec 07 '22 23:12

Peter Knego