Skip to main content

Upload File with Form Data in Java

This is simple Java code to upload file with form data 

Jar file Used: Apache Http Client 

package com.james.util.file.upload;


import java.io.File;
import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class FileUpload {
   
        public static void main(String[] args) throws ClientProtocolException, IOException {
           
            String  ticket="62cd28e08e398be82e38a064ed";
            String url = "?ticket=";
            String contentPath = "/home/james/sqltips.pdf";
            String desc  = "Description Text";
            String schemaType  = "Savings";
            String customerName = "james";
           
            String response = uploadFile(url,ticket,desc,
schemaType,customerName,contentPath);
          
            System.out.println("Response From Server   "+response);
        }

       

        public static String uploadFile(String url, String ticket, String desc, String
schemaType, String customerName,String contentPath) throws ClientProtocolException, IOException
        {
            String response = null;

  
            HttpPost post = new HttpPost(url+ticket); //Just for testing purpose please use StringBuilder
           
            File file = new File(contentPath);
             FileBody fileBody = new FileBody(file);
           
            CloseableHttpClient httpclient = HttpClients.createDefault();
           
            StringBody strBody_desc  = new StringBody(desc, ContentType.MULTIPART_FORM_DATA);
            StringBody strBody_
schemaType  = new StringBody(schemaType, ContentType.MULTIPART_FORM_DATA);
            StringBody strBody_customerName = new StringBody(customerName, ContentType.MULTIPART_FORM_DATA);          
           
        
           
            HttpEntity reqEntity = MultipartEntityBuilder.create()
                    .addPart("file", fileBody)
                    .addPart("description", strBody_desc)
                    .addPart("
schemaType", strBody_schemaType)
                    .addPart("customerName", strBody_customerName)
                    .build();
           
            post.setEntity(reqEntity);
           
           
            CloseableHttpResponse httpResponse = httpclient.execute(post);
            try {
                HttpEntity entity1 = httpResponse.getEntity();
                if (entity1 != null) {
                    response = EntityUtils.toString(entity1);
                } else {


                               //Print other message
                           }
                } finally {
                    httpResponse.close();
                }
         
            return response;
        }
       
}

Comments