0

So, I can't get rid of this problem: I need to upload a XML file and a .jpg file from my android app(API 8) to a HTTP server (win 2008 server with IIS 7.5). I've already enabled PUT verbs and uninstalled WebDav & webdav methods as suggested from previous searches. Plus, i'm not sure i'm doing it right server side because i can't get any response back.

here's my code

        URL fileurl = new URL("Server Upload Path");

        HttpURLConnection urlConnection = (HttpURLConnection) fileurl
                .openConnection();
        urlConnection.setRequestMethod("PUT");
        urlConnection.setDoOutput(true);
        urlConnection.connect();

        OutputStream os = urlConnection.getOutputStream();

        File upFile = new File("My Local File");
                    //I'm sure the file exists
        FileInputStream fis = new FileInputStream(upFile);
        BufferedInputStream bfis = new BufferedInputStream(fis);
        byte[] buffer = new byte[1024];
        int bufferLength = 0;

        // now, read through the input buffer and write the contents to the
        // file
        while ((bufferLength = bfis.read(buffer)) > 0) {
            os.write(buffer, 0, bufferLength);

        }

Sorry if I forget some info you may need to help. I'm new to android and IIS too.

CAMOBAP
  • 5,523
  • 8
  • 58
  • 93
JJonDuty
  • 123
  • 2
  • 7
  • First test out your server side and make sure it's working, don't even worry about the client until you can validate that. Use curl from the command line, or hurl.it: http://www.hurl.it/ and so on. – Charlie Collins Jan 17 '13 at 16:58

1 Answers1

-1

Why not try a standard multi-part file upload request (based on POST and not PUT):

     final static String MULTIPART_BOUNDARY = "------------------563i2ndDfv2rTHiSsdfsdbouNdArYfORhxcvxcvefj3q2f";

 public static void sendFileToServer(String url, File logFiles) {
    HttpURLConnection connection = null;
    OutputStream os = null;
    DataInputStream is = null;
try {
    StringBuilder fullUrl = new StringBuilder(url);
    connection = (HttpURLConnection) new URL(url).openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + MULTIPART_BOUNDARY);
    connection.setDoOutput(true);
    connection.setDoInput(true);
    connection.setRequestProperty("Connection", "Keep-Alive");
    connection.connect();
    os = new BufferedOutputStream(connection.getOutputStream());
    if(os != null) {
          os.write(("--" + MULTIPART_BOUNDARY + EOL).getBytes());
          os.write(String.format("Content-Disposition:form-data;name=\"UploadedFile\";filename=\"%s\"\r\nContent-Type: application/x-zip-compressed\r\n\r\n", UPLOADED_FILE_NAME).getBytes());

          // Upload file(s) data here and send

          os.write((EOL + "--" + MULTIPART_BOUNDARY + "--" + EOL + EOL).getBytes());
          os.flush();
          os.close();
          os = null;
       }
       if(connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                // Process server response
       }
   } catch (MalformedURLException e) {


   } catch (IOException e) {


   } catch (Exception e1) {

   } finally {
    try {
        if(os != null) {
            os.close();
        }
    } catch (IOException e) {
        Log.e(TAG, "sendFileToServer exception: close OutputStream", e);
    }
    try {
        if(is != null) {
            is.close();
        }
    } catch (IOException e) {
        Log.e(TAG, "sendFileToServer exception: close InputStream", e);
    }
    if(connection != null) {
        connection.disconnect();
    }
   }
}
  • Do i need some asp or php server side? – JJonDuty Jan 18 '13 at 08:38
  • The server needs to support the standard file upload RFC. I think most servers support this out of the box. if you need some special handling, you may need a server side page (php or asp or servlet - your choice). In the sample above I sent a compressed file content so I had a server side page to decompress it. – Miron Ophir Jan 20 '13 at 07:51
  • I have an asp called by a htm page. With this solution the server returns me a 411 Lenght required error. Is there a way to set it? – JJonDuty Jan 21 '13 at 08:39