ByteArrayOutputStream to a FileBody

12,115

Solution 1

Use a ByteArrayBody instead (available since HTTPClient 4.1), despite its name it takes a file name, too:

ContentBody mimePart = new ByteArrayBody(bos.toByteArray(), "filename");

If you are stuck with HTTPClient 4.0, use InputStreamBody instead:

InputStream in = new ByteArrayInputStream(bos.toByteArray());
ContentBody mimePart = new InputStreamBody(in, "filename") 

(Both classes also have constructors that take an addtional MIME type string)

Solution 2

i hope it may help some one , you can mention the file type as "image/jpeg" in FileBody as below code

HttpClient httpClient = new DefaultHttpClient();
            HttpPost postRequest = new HttpPost(
                    "url");
            MultipartEntity reqEntity = new MultipartEntity(
                    HttpMultipartMode.BROWSER_COMPATIBLE);
            reqEntity.addPart("name", new StringBody(name));
            reqEntity.addPart("password", new StringBody(pass));
File file=new File("/mnt/sdcard/4.jpg");
ContentBody cbFile = new FileBody(file, "image/jpeg");
reqEntity.addPart("file", cbFile);
    postRequest.setEntity(reqEntity);
            HttpResponse response = httpClient.execute(postRequest);
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(
                            response.getEntity().getContent(), "UTF-8"));
            String sResponse;
            StringBuilder s = new StringBuilder();
            while ((sResponse = reader.readLine()) != null) {
                s = s.append(sResponse);
            }

            Log.e("Response for POst", s.toString());

need to add jar files httpclient-4.2.2.jar,httpmime-4.2.2.jar in your project.

Share:
12,115
Keith Adler
Author by

Keith Adler

Nearly 30 years of multi-national IT consulting and software engineering experience in diverse industries such as entertainment, insurance, energy, utilities, healthcare, real estate, advertising, government, education and financial services applying knowledge and expertise using a range of methodologies, tools, vendors and platforms. Experience includes Cloud IaaS/PaaS DevOps/SysOps, web-based solutions architecture & development on a variety of platforms (Java, .NET, Python), automation and event management (Ansible/New Relic), infrastructure planning & implementation, quality assurance testing, project management and information security. Professional experience is complimented by vendor-neutral technical and business qualifications.

Updated on June 16, 2022

Comments

  • Keith Adler
    Keith Adler almost 2 years

    I have a Uri to an image that was either taken or selected from the Gallery that I want to load up and compress as a JPEG with 75% quality. I believe I have achieved that with the following code:

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    Bitmap bm = BitmapFactory.decodeFile(imageUri.getPath());
    bm.compress(CompressFormat.JPEG, 60, bos);
    

    Not that I have tucked it into a ByteArrayOutputStream called bos I need to then add it to a MultipartEntity in order to HTTP POST it to a website. What I can't figure out is how to convert the ByteArrayOutputStream to a FileBody.