Here you can find the source of writeData(HttpURLConnection conn, String boundary, File file, String mimeType, String fieldName)
private static void writeData(HttpURLConnection conn, String boundary, File file, String mimeType, String fieldName) throws IOException
//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(); } }