In this post, I will share a sample android (java) code to upload image to server using multipart form data.
 import java.io.ByteArrayOutputStream;  
 import java.io.InputStream;  
 import java.io.OutputStream;  
 import java.net.HttpURLConnection;  
 import java.net.URL;  
 /**  
  * Created by ITJONBOT on 2/28/2017.  
  */  
 public class KHttpClient {  
   String url;  
   private HttpURLConnection con;  
   private OutputStream os;  
   private String delimiter = "--";  
   private String boundary = "SwA"+Long.toString(System.currentTimeMillis())+"SwA";  
   public KHttpClient(String url) {  
     this.url = url;  
   }  
   public byte[] downloadImage(String imgName) {  
     ByteArrayOutputStream baos = new ByteArrayOutputStream();  
     try {  
       System.out.println("URL ["+url+"] - Name ["+imgName+"]");  
       HttpURLConnection con = (HttpURLConnection) ( new URL(url)).openConnection();  
       con.setRequestMethod("POST");  
       con.setDoInput(true);  
       con.setDoOutput(true);  
       con.connect();  
       con.getOutputStream().write( ("name=" + imgName).getBytes());  
       InputStream is = con.getInputStream();  
       byte[] b = new byte[1024];  
       while ( is.read(b) != -1)  
         baos.write(b);  
       con.disconnect();  
     }  
     catch(Throwable t) {  
       t.printStackTrace();  
     }  
     return baos.toByteArray();  
   }  
   public void connectForMultipart() throws Exception {  
     con = (HttpURLConnection) ( new URL(url)).openConnection();  
     con.setRequestMethod("POST");  
     con.setDoInput(true);  
     con.setDoOutput(true);  
     con.setRequestProperty("Connection", "Keep-Alive");  
     con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);  
     con.connect();  
     os = con.getOutputStream();  
   }  
   public void addFormPart(String paramName, String value) throws Exception {  
     writeParamData(paramName, value);  
   }  
   public void addFilePart(String paramName, String fileName, byte[] data) throws Exception {  
     os.write( (delimiter + boundary + "\r\n").getBytes());  
     os.write( ("Content-Disposition: form-data; name=\"" + paramName + "\"; filename=\"" + fileName + "\"\r\n" ).getBytes());  
     os.write( ("Content-Type: application/octet-stream\r\n" ).getBytes());  
     os.write( ("Content-Transfer-Encoding: binary\r\n" ).getBytes());  
     os.write("\r\n".getBytes());  
     os.write(data);  
     os.write("\r\n".getBytes());  
   }  
   public void finishMultipart() throws Exception {  
     os.write( (delimiter + boundary + delimiter + "\r\n").getBytes());  
   }  
   public String getResponse() throws Exception {  
     InputStream is = con.getInputStream();  
     byte[] b1 = new byte[1024];  
     StringBuffer buffer = new StringBuffer();  
     while ( is.read(b1) != -1)  
       buffer.append(new String(b1));  
     con.disconnect();  
     return buffer.toString();  
   }  
   private void writeParamData(String paramName, String value) throws Exception {  
     os.write( (delimiter + boundary + "\r\n").getBytes());  
     os.write( "Content-Type: text/plain\r\n".getBytes());  
     os.write( ("Content-Disposition: form-data; name=\"" + paramName + "\"\r\n").getBytes());;  
     os.write( ("\r\n" + value + "\r\n").getBytes());  
   }  
 }  

USAGE
 
BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inDensity = DisplayMetrics.DENSITY_DEFAULT;
        opt.inTargetDensity = DisplayMetrics.DENSITY_DEFAULT;
        opt.inScaled = false;
        Bitmap b = BitmapFactory.decodeFile(mFile.getPath(),opt);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        b.compress(Bitmap.CompressFormat.JPEG, 50, baos);
  
KHttpClient client = new KHttpClient(url);  
         client.connectForMultipart();  
         client.addFormPart("param1", param1); // Aditional Text Param #1  
         client.addFormPart("param2", param2); // Aditional Text Param #1  
         client.addFilePart("photo", "aa.png", baos.toByteArray()); // File  
         client.finishMultipart();  
         String data = client.getResponse();  
         Log.d("RESPONSE ::: ", data);  

►►សូមអរគុណរាល់ការចូលរួមCommentរបស់អ្នក!

 
Top
Don't You Think this Awesome Post should be shared ??
| Android Upload Image To Server As Multipart Form |