Android Http Post writeData(HttpURLConnection conn, String boundary, File file, String mimeType, String fieldName)

Here you can find the source of writeData(HttpURLConnection conn, String boundary, File file, String mimeType, String fieldName)

Description

write Data

Declaration

private static void writeData(HttpURLConnection conn, String boundary,
            File file, String mimeType, String fieldName)
            throws IOException 

Method Source Code

//package com.java2s;

import java.io.*;
import java.net.HttpURLConnection;

public class Main {
    private static final String CRLF = "\r\n";

    private static void writeData(HttpURLConnection conn, String boundary,
            File file, String mimeType, String fieldName)
            throws IOException {
        DataOutputStream requestData = new DataOutputStream(
                conn.getOutputStream());

        requestData.writeBytes("--" + boundary + CRLF);
        requestData.writeBytes("Content-Disposition: form-data; name=\""
                + fieldName + "\"; filename=\"" + file.getName() + "\""
                + CRLF);/*from  w w  w .  j  a v a 2  s.  c  o  m*/
        requestData.writeBytes("Content-Type: " + mimeType + CRLF + CRLF);

        InputStream fileInput = new FileInputStream(file);
        int bytesRead;
        byte[] buffer = new byte[1024];
        while ((bytesRead = fileInput.read(buffer)) != -1) {
            requestData.write(buffer, 0, bytesRead);
        }
        fileInput.close();
        requestData.writeBytes(CRLF);

        requestData.writeBytes("--" + boundary + "--" + CRLF);
        requestData.flush();
    }
}

Related

  1. httpPost(String... params)
  2. prepareAndSendHttpPost(String URI, ArrayList params)
  3. buildNameValuePair( Hashtable httpPost)
  4. sendPostRequest(String path, String params, String encoding)
  5. executePost(String url, String parameters)