Search code examples
androidasp.net-web-apimultipartform-dataform-data

How to post a multi part form data from android to web api?


I want to post exactly show in the picture. From postman its working good.I don't know how to do this in android. TIA

Check the image for my request in postman: https://i.sstatic.net/2lc3m.png


Solution

  • There are many post about multipart on stackoverflow,try this

    public static String uploadMultipartFile(String root, String token,
                             String username, String sourceFileUri,
                             String fileStyle)
    {
        HttpURLConnection connection = null;
        DataOutputStream outputStream = null;
        String pathToOurFile = sourceFileUri;
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";
        StringBuffer response = new StringBuffer();
        try
        {
            FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile));
            URL url = new URL(root);
            connection = (HttpURLConnection) url.openConnection();
    
            if (token != null)
            {
                connection.setRequestProperty("Authorization", "Basic " + token);
            }
            if (username != null)
            {
                connection.setRequestProperty("Username", username);
            }
            if (fileStyle != null)
            {
                connection.setRequestProperty("file-type", fileStyle);
            }
    
            String fileExtension = FilenameUtils.getExtension(sourceFileUri);
            String mime = Utils.getFileMIME(fileExtension);
    
            Log.d("uploadMultipartFile","fileExtension:"+fileExtension+",mime:"+mime);
    
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setUseCaches(false);
    
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("Cache-Control", "no-cache");
            connection.setRequestProperty("User-Agent", "CodeJava Agent");
            connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
    
            outputStream = new DataOutputStream(connection.getOutputStream());
            outputStream.writeBytes(twoHyphens + boundary + lineEnd);
            outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
                    + pathToOurFile + "\"" + lineEnd);
            outputStream.writeBytes("Content-Type: "+ mime + lineEnd);
    
            outputStream.writeBytes(lineEnd);
    
            byte[]bytes = IOUtils.toByteArray(fileInputStream);
            outputStream.write(bytes);
    
            outputStream.writeBytes(lineEnd);
            outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
    
            int serverResponseCode = connection.getResponseCode();
            String serverResponseMessage = connection.getResponseMessage();
            Log.i(HttpUtil.class.getSimpleName(), String.valueOf(serverResponseCode));
            Log.i(HttpUtil.class.getSimpleName(), serverResponseMessage);
    
            fileInputStream.close();
            outputStream.flush();
            outputStream.close();
    
            BufferedReader br=null;
            if(connection.getResponseCode()>=200 && connection.getResponseCode()<300)
            {
                br = new BufferedReader(new InputStreamReader((connection.getInputStream())));
    
            }
            else if(connection.getResponseCode()>=400)
            {
                br = new BufferedReader(new InputStreamReader((connection.getErrorStream())));
            }
            String inputLine;
            while ((inputLine = br.readLine()) != null) {
                response.append(inputLine);
            }
            System.out.println("result from server:"+response.toString());
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        return response.toString();
    }