Question

I am trying to attach files through Java code to http://example.com:8080/attachments. I am using multipart data upload. I tried with uploading text files and its working fine. When I tried jpg files the response code is 200, but the uploaded file is having some problems. It says the file is corrupted.

The code for converting the image to byte array.

             byte[] bFile = new byte[(int) file.length()];
             fileInputStream = new FileInputStream(file);
             int bytesAvailable = fileInputStream.available();
             int maxBufferSize = 4096;

             byte[] buffer = new byte[bytesAvailable];
             ByteArrayOutputStream bos = new ByteArrayOutputStream();

             int bytesRead = fileInputStream.read(buffer, 0, bytesAvailable);

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

The code for uploading

        String contentDisposition = "Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getName() + "\"";
        String contentType = "Content-Type: image/jpg";         
        String BOUNDARY = "*****";
        HttpsURLConnection connection;
        String CRLF = "\r\n";   
        StringBuffer requestBody = new StringBuffer();
        requestBody.append("--");
        requestBody.append(BOUNDARY);
        requestBody.append(CRLF);
        requestBody.append(contentDisposition);
        requestBody.append(CRLF);
        requestBody.append(contentType);
        requestBody.append(CRLF);
        requestBody.append(CRLF);
        requestBody.append(new String(bFile));
        requestBody.append(CRLF);
        requestBody.append("--");
        requestBody.append(BOUNDARY);
        requestBody.append("--");

        URL obj = new URL("url");
        connection = (HttpsURLConnection) obj.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
        connection.setRequestProperty("User-Agent", "Mozilla/5.0");
        connection.setRequestProperty("Accept","*/*");
        connection.setRequestProperty("Authorization", strEncoded);
        connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);

        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setUseCaches(false);
        DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
        wr.writeBytes(requestBody.toString());
        wr.flush();
        wr.close();

Please let me know if there is any problem with the byte array I am sending for images.

Please check the screen shot attached for the error message.

http://tinypic.com/r/33233hl/5

Thanks.

Was it helpful?

Solution

Your code for reading file bytes is error-prone, have a look at the answers for this question.

You can not just add the bytes in the request-body with
requestBody.append(new String(bFile));
This will convert the image bytes to characters which is not what you want - you need the bytes transferred as you read them from the image-file.

Instead of doing this manually, use something like Apache HttpClient. It makes takes care of details like properly putting file-bytes in an http-post. See the official example or see the code in this question.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top