44

I have very little idea how html works.What i want to do is exactly similar to the following but on android

<body>
    <form action="<%= some_url %>" method="post" enctype="multipart/form-data">
        <input type="file" name="myFile">
        <input type="submit" value="Submit">
    </form>
</body>

I tried the following code -

private static void postToUrl(String url_to_upload_on,
        String file_name_with_ext, byte[] byteArray) {

    String attachmentName = "file";
    String attachmentFileName = file_name_with_ext;
    String crlf = "\r\n";
    String twoHyphens = "--";
    String boundary =  "*****";

    try{

    URL url = new URL(url_to_upload_on);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoOutput(true);
    connection.setRequestMethod("POST");

    connection.setRequestProperty(
        "Content-Type", "multipart/form-data;boundary=" + boundary);
    DataOutputStream request = new DataOutputStream(
            connection.getOutputStream()); 
    request.writeBytes(twoHyphens + boundary + crlf);
    request.writeBytes("Content-Disposition: form-data; name=\"" +
        attachmentName + "\";filename=\"" + 
        attachmentFileName + "\"" + crlf);
    request.writeBytes(crlf);
    request.write(byteArray);
    request.writeBytes(crlf);
    request.writeBytes(twoHyphens + boundary + 
        twoHyphens + crlf);
    request.flush();
    request.close();

    }catch(Exception e){
        e.printStackTrace();
    }


}

this gives me no direct errors but when i get error-stream using-

 Log.w(TAG, "connection.getErrorStream() = " +      connection.getErrorStream());

i get this-

12-14 18:25:54.911: W/uploadToBlobStore(30558): httpUrlConnection.getErrorStream() = com.android.okhttp.internal.http.HttpTransport$FixedLengthInputStream@426dd5a8

with no success.

PS- I am uploading a file to google blobstore

PS- I can not use Apache http libraries or its multipart class as android says its depreciated

EDIT 1

Now I am using the following code but it is working only for files less then 2.3Mb -

private static void postToUrl3(String url_to_upload_on,
        String file_name_with_ext, byte[] byteArray, String mimeType) {

    CloseableHttpClient httpClient = null;

    try {

        httpClient = HttpClientBuilder.create().build();

        HttpPost postRequest = new HttpPost(url_to_upload_on);


        MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create();
        reqEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);            
        ByteArrayBody bab = new ByteArrayBody(byteArray, file_name_with_ext);           
        reqEntity.addPart("file", bab);         
        postRequest.setEntity(reqEntity.build());


        httpClient.execute(postRequest);// takes time

    } catch (Exception e) {
        Log.w("uploadToBlobStore", "postToUrl Exception e = " + e);
        e.printStackTrace();
    } finally {
        if (httpClient != null) {
            Log.w("uploadToBlobStore", "connection.closing ");
            try {
                httpClient.close();
            } catch (IOException e) {
                Log.w("uploadToBlobStore", "connection.closing errot e = "
                        + e);
                e.printStackTrace();
            }
        }
    }
}

how to make it work with larger files?

PS- i am sending it to blobstore and i did set the maxUploadSizeBytesand MaxUploadSizeBytesPerBlob to 30MB.I am not able to figure out the issue with size because google blobstore documentation says -

Google App Engine includes the Blobstore service, which allows applications to serve data objects limited only by the amount of data that can be uploaded or downloaded over a single HTTP connection.

So can it be a problem with http connection? and if so, how can i configure it.

Flying Monkey
  • 629
  • 1
  • 5
  • 12
  • I was able to upload a file using multiPartEntity, check out the code I posted in the answer here: https://stackoverflow.com/questions/34222980/urlconnection-always-returns-400-bad-request-when-i-try-to-upload-a-wav-file Tick green if it works! cheers! – Josh Dec 17 '15 at 13:26

4 Answers4

84

I am using HttpURLConnection to achieve this.

Create one Multipart custom class ::

public class MultipartUtility {
    private final String boundary;
    private static final String LINE_FEED = "\r\n";
    private HttpURLConnection httpConn;
    private String charset;
    private OutputStream outputStream;
    private PrintWriter writer;

    /**
     * This constructor initializes a new HTTP POST request with content type
     * is set to multipart/form-data
     *
     * @param requestURL
     * @param charset
     * @throws IOException
     */
    public MultipartUtility(String requestURL, String charset)
            throws IOException {
        this.charset = charset;

        // creates a unique boundary based on time stamp
        boundary = "===" + System.currentTimeMillis() + "===";
        URL url = new URL(requestURL);
        httpConn = (HttpURLConnection) url.openConnection();
        httpConn.setUseCaches(false);
        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);
    }

    /**
     * Adds a form field to the request
     *
     * @param name  field name
     * @param value field value
     */
    public void addFormField(String name, String value) {
        writer.append("--" + boundary).append(LINE_FEED);
        writer.append("Content-Disposition: form-data; name=\"" + name + "\"")
                .append(LINE_FEED);
        writer.append("Content-Type: text/plain; charset=" + charset).append(
                LINE_FEED);
        writer.append(LINE_FEED);
        writer.append(value).append(LINE_FEED);
        writer.flush();
    }

    /**
     * Adds a upload file section to the request
     *
     * @param fieldName  name attribute in <input type="file" name="..." />
     * @param uploadFile a File to be uploaded
     * @throws IOException
     */
    public void addFilePart(String fieldName, File uploadFile)
            throws IOException {
        String fileName = uploadFile.getName();
        writer.append("--" + boundary).append(LINE_FEED);
        writer.append(
                "Content-Disposition: form-data; name=\"" + fieldName
                        + "\"; filename=\"" + fileName + "\"")
                .append(LINE_FEED);
        writer.append(
                "Content-Type: "
                        + URLConnection.guessContentTypeFromName(fileName))
                .append(LINE_FEED);
        writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED);
        writer.append(LINE_FEED);
        writer.flush();

        FileInputStream inputStream = new FileInputStream(uploadFile);
        byte[] buffer = new byte[4096];
        int bytesRead = -1;
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }
        outputStream.flush();
        inputStream.close();
        writer.append(LINE_FEED);
        writer.flush();
    }

    /**
     * Adds a header field to the request.
     *
     * @param name  - name of the header field
     * @param value - value of the header field
     */
    public void addHeaderField(String name, String value) {
        writer.append(name + ": " + value).append(LINE_FEED);
        writer.flush();
    }

    /**
     * Completes the request and receives response from the server.
     *
     * @return a list of Strings as response in case the server returned
     * status OK, otherwise an exception is thrown.
     * @throws IOException
     */
    public List<String> finish() throws IOException {
        List<String> response = new ArrayList<String>();
        writer.append(LINE_FEED).flush();
        writer.append("--" + boundary + "--").append(LINE_FEED);
        writer.close();

        // checks server's status code first
        int status = httpConn.getResponseCode();
        if (status == HttpURLConnection.HTTP_OK) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    httpConn.getInputStream()));
            String line = null;
            while ((line = reader.readLine()) != null) {
                response.add(line);
            }
            reader.close();
            httpConn.disconnect();
        } else {
            throw new IOException("Server returned non-OK status: " + status);
        }
        return response;
    }
}

Use it (async way) ::

    MultipartUtility multipart = new MultipartUtility(requestURL, charset);

    // In your case you are not adding form data so ignore this
                /*This is to add parameter values */
                for (int i = 0; i < myFormDataArray.size(); i++) {
                    multipart.addFormField(myFormDataArray.get(i).getParamName(),
                            myFormDataArray.get(i).getParamValue());
                }


//add your file here.
                /*This is to add file content*/
                for (int i = 0; i < myFileArray.size(); i++) {
                    multipart.addFilePart(myFileArray.getParamName(),
                            new File(myFileArray.getFileName()));
                }

                List<String> response = multipart.finish();
                Debug.e(TAG, "SERVER REPLIED:");
                for (String line : response) {
                    Debug.e(TAG, "Upload Files Response:::" + line);
// get your server response here.
                    responseString = line;
                }
KDeogharkar
  • 11,502
  • 7
  • 51
  • 93
  • Thank you so much for sharing this. It's taken me a lot of searching to find an alternative to `org.apache.http.entity.mime.MultipartEntity` and your code works a treat. :-) – ban-geoengineering Feb 20 '16 at 15:43
  • Welcome @ban-geoengineering – KDeogharkar Feb 20 '16 at 16:28
  • 4
    I have just found an issue with this code. The `addFormField` method is adding a line break to the actual value that is sent to the server. To make it work as intended, I have removed the final `.append(LINE_FEED)` from that method. Cheers, – ban-geoengineering Mar 16 '16 at 18:57
  • 3
    ...I ran into problems with that fix (the server throwing a 400 error). A better solution seems to be to replace `writer.append(value).append(LINE_FEED); writer.flush();` with `writer.append(value); writer.flush(); writer.append(LINE_FEED);` – ban-geoengineering Mar 17 '16 at 12:29
  • What should be the value of "charset"? – Shajeel Afzal May 27 '16 at 06:40
  • 1
    @ShajeelAfzal . it should be character set like UTF-8,UTF-16 whatever your need is. – KDeogharkar May 27 '16 at 06:43
  • Thanks, Your answer really helpful – Sruit A.Suk Jun 18 '16 at 07:53
  • @KDeogharkar How did you get the getParamName() method? Did you have to write your own getter? – Akilan Arasu Aug 22 '16 at 04:16
  • yes . you can create you own custom class to set and get key value that you want to pass. @AkilanArasu – KDeogharkar Aug 22 '16 at 05:03
  • Thanks, your answer was very useful. – Akilan Arasu Aug 22 '16 at 09:58
  • Its ignoring "\n" in addFormField. When sending a text containing "\n" its replacing "\n" with a space. Please let me know whats the issue. – Vikrant_Dev Dec 01 '16 at 09:06
  • 3
    the === in the boundary was creating an error in golang.(panic: mime: invalid media parameter). I had to change it to --- to resolve the issue – techblu3 Dec 07 '16 at 10:22
  • I had to comment out two LINE_FEED in the finish() method to make this code work. – Hashim Akhtar Feb 14 '17 at 12:22
  • 3
    Just change the boundary === to anything else like: ***** and it works perfectly. – Guilherme Simão Couto Mar 22 '18 at 14:31
  • @GuilhermeSimãoCouto Thank you very much bro i wasted whole day..............Thanks lot bro for your valuable comments...thank you once again.....+100 – demo Aug 08 '18 at 13:46
  • 1
    Thanks a lot! But headers don't work (they need to be set as a `httpConn.setRequestProperty` in the constructor), and response codes > 400 don't work either (because those give a `httpConn.getErrorStream()`, not a `httpConn.getInputStream()`). – haslo Jun 12 '19 at 12:43
8

use okhttp and use following snippet (taken from recipes)

adjust the header values according to what your server expects.

private static final String IMGUR_CLIENT_ID = "...";
private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");

private final OkHttpClient client = new OkHttpClient();

public void run() throws Exception {
// Use the imgur image upload API as documented at https://api.imgur.com/endpoints/image
    RequestBody requestBody = new MultipartBuilder()
    .type(MultipartBuilder.FORM)
    .addPart(
        Headers.of("Content-Disposition", "form-data; name=\"title\""),
        RequestBody.create(null, "Square Logo"))
    .addPart(
        Headers.of("Content-Disposition", "form-data; name=\"image\""),
        RequestBody.create(MEDIA_TYPE_PNG, new File("website/static/logo-square.png")))
    .build();

Request request = new Request.Builder()
    .header("Authorization", "Client-ID " + IMGUR_CLIENT_ID)
    .url("https://api.imgur.com/3/image")
    .post(requestBody)
    .build();

Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

System.out.println(response.body().string());
}
eriuzo
  • 1,677
  • 1
  • 14
  • 16
  • sorry for late reply.Do i have to add this 'okHttp' as an external library? – Flying Monkey Dec 18 '15 at 11:47
  • ok i added okHttp using jar. but eclipse gives me error "The type okio.ByteString cannot be resolved. It is indirectly referenced from required .class files" at 'RequestBody.create(MEDIA_TYPE_PNG, new File("website/static/logo-square.png")))' – Flying Monkey Dec 18 '15 at 15:25
  • @FlyingMonkey I suggest you use Android Studio.. if you absolutely need to use eclipse however, download the okio jar (https://github.com/square/okio) and add it the same way you added okhttp – eriuzo Dec 18 '15 at 22:45
  • yes, don't use Eclipse, it's been discontinued by Android SDK. You don't need to add any jars manually in Android Studio, just write a line in your gradle file. Use okhttp with retrofit to make the calls much more simple. http://square.github.io/retrofit/ http://square.github.io/okhttp/ – headsvk Dec 23 '15 at 21:05
  • 1
    THANKYOU! This code is not the best solution, but the pointer to `okhttp` is! I just want to emphasise to anyone battling this problem of uploading a file from Android that okhttp (currently okhttp3) is the way to go! When I started on this and saw okhttp mentioned I was wary of it, because one is always reluctant to use a free library you've never heard of. However, having been forced to this solution, I can't recommend okhttp too highly. It looks to have a strong maintenance record, is easy to use and install (into AndroidStudio), and solves a problem which other methods didn't (for me). – Stephen Hosking Dec 14 '16 at 21:18
  • For current and neater solutions with okhttp as of Dec 2016, see http://stackoverflow.com/questions/23512547/how-to-use-okhttp-to-upload-a-file/30498514#30498514 – Stephen Hosking Dec 14 '16 at 21:20
  • 1
    @StephenHosking [internally android has been using okhttp since 4.4 that's why i recommend using it](https://twitter.com/JakeWharton/status/482563299511250944) – eriuzo Dec 16 '16 at 08:13
5

As alternative you can use Retrofit.

You can specify a call like this:

@Multipart
@POST("/user/photo")
Call<User> updateUser(@Part("photo") RequestBody photo, @Part("description") RequestBody description);

then create it like this:

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("https://api.github.com")
    .build();
GitHubService service = retrofit.create(GitHubService.class);

and finally execute it like this:

service.updateUser(Photo, description).enqueue() --> asynchronous

service.updateUser(Photo, description).execute() --> synchronous

See the documentation here

Amir
  • 13,841
  • 10
  • 67
  • 104
Lars Celie
  • 592
  • 4
  • 15
-2

Volley is a good http library for multipart data. AndroidMultiPartEntity class is for progress listener.

    public class AndroidMultiPartEntity extends MultipartEntity

    {

    private final ProgressListener listener;

    public AndroidMultiPartEntity(final ProgressListener listener) {
    super();
    this.listener = listener;
    }

    public AndroidMultiPartEntity(final HttpMultipartMode mode, final ProgressListener listener) {
    super(mode);
    this.listener = listener;
    }

    public AndroidMultiPartEntity(HttpMultipartMode mode, final String boundary,
    final Charset charset, final ProgressListener listener) {
    super(mode, boundary, charset);
    this.listener = listener;
    }

    @Override
    public void writeTo(final OutputStream outstream) throws IOException {
    super.writeTo(new CountingOutputStream(outstream, this.listener));
    }

    public static interface ProgressListener {
    void transferred(long num);
    }

    public static class CountingOutputStream extends FilterOutputStream {

    private final ProgressListener listener;
    private long transferred;

    public CountingOutputStream(final OutputStream out,
                                final ProgressListener listener) {
        super(out);
        this.listener = listener;
        this.transferred = 0;
    }

    public void write(byte[] b, int off, int len) throws IOException {
        out.write(b, off, len);
        this.transferred += len;
        this.listener.transferred(this.transferred);
    }

    public void write(int b) throws IOException {
        out.write(b);
        this.transferred++;
        this.listener.transferred(this.transferred);
    }
    }
    }

    Call the Async task like this
    new UploadFileToServer().execute();


    The Call method:
    private class UploadFileToServer extends AsyncTask<Void, Integer, String> {
    @Override
    protected void onPreExecute() {

        super.onPreExecute();
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {

    }

    @Override
    protected String doInBackground(Void... params) {

        return uploadFile();

    }

    private String uploadFile() {

        String responseString = null;

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(Config.Seeker_Image_Upload);

        try {
            AndroidMultiPartEntity entity = new AndroidMultiPartEntity(new AndroidMultiPartEntity.ProgressListener() {

                @Override
                public void transferred(long num) {
                    publishProgress((int) ((num / (float) totalSize) * 100));
                }
            });

            File sourceFile = new File(Path);

            // Adding file data to http body
            entity.addPart("logo", new FileBody(sourceFile));

            // Extra parameters if you want to pass to server
            //entity.addPart("website", new StringBody("www.androidhive.info"));

            // String emailaddress = UserActivity.emailaddress;

            /*preferences = SeekerProfile.this.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
            email_address = preferences.getString("EMAILADDRESS", "");*/
            entity.addPart("EMAILADDRESS", new StringBody(email_address));
            entity.addPart("OPER", new StringBody(Operation_recruiter_logo_upload));

            totalSize = entity.getContentLength();
            httppost.setEntity(entity);

            // Making server call
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity r_entity = response.getEntity();

            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                // Server response
                responseString = EntityUtils.toString(r_entity);
            } else {
                responseString = "Error occurred! Http Status Code: " + statusCode;
            }

        } catch (ClientProtocolException e) {
            responseString = e.toString();
        } catch (IOException e) {
            responseString = e.toString();
        }

        return responseString;

        }

        @Override
        protected void onPostExecute(String result) {
        //Log.e(TAG, "Response from server: " + result);
        enter code here
        }

    }
MWiesner
  • 7,913
  • 11
  • 31
  • 66
Ramesh sambu
  • 3,285
  • 1
  • 21
  • 37