Example usage for org.apache.commons.imaging ImageInfo getMimeType

List of usage examples for org.apache.commons.imaging ImageInfo getMimeType

Introduction

In this page you can find the example usage for org.apache.commons.imaging ImageInfo getMimeType.

Prototype

public String getMimeType() 

Source Link

Document

Returns the MIME type of the image.

Usage

From source file:com.imagesleuth.imagesleuthclient2.Poster.java

public String Post(final File imgFile) throws IOException, ImageReadException, InterruptedException {

    final ArrayList<String> idArray = new ArrayList<>();

    final CountDownLatch latch = new CountDownLatch(1);

    try (CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom().setDefaultHeaders(harray)
            .setDefaultCredentialsProvider(credsProvider).setDefaultRequestConfig(requestConfig).build()) {
        httpclient.start();//from   ww w .  j  a v a  2s  . c o m
        final byte[] imageAsBytes = IOUtils.toByteArray(new FileInputStream(imgFile));
        final ImageInfo info = Imaging.getImageInfo(imageAsBytes);
        final HttpPost request = new HttpPost(urlval);
        String boundary = UUID.randomUUID().toString();
        HttpEntity mpEntity = MultipartEntityBuilder.create().setBoundary("-------------" + boundary)
                .addBinaryBody("file", imageAsBytes, ContentType.create(info.getMimeType()), imgFile.getName())
                .build();
        ByteArrayOutputStream baoStream = new ByteArrayOutputStream();
        mpEntity.writeTo(baoStream);
        request.setHeader("Content-Type", "multipart/form-data;boundary=-------------" + boundary);
        //equest.setHeader("Content-Type", "multipart/form-data");                               
        NByteArrayEntity entity = new NByteArrayEntity(baoStream.toByteArray(),
                ContentType.MULTIPART_FORM_DATA);
        request.setEntity(entity);
        httpclient.execute(request, new FutureCallback<HttpResponse>() {

            @Override
            public void completed(final HttpResponse response) {
                int code = response.getStatusLine().getStatusCode();
                //System.out.println(" response code: " + code + " for image: " + imgFile.getName());
                if (response.getEntity() != null && code == 202) {
                    StringWriter writer = new StringWriter();
                    try {
                        IOUtils.copy(response.getEntity().getContent(), writer);
                        idArray.add(writer.toString());
                        writer.close();
                        //System.out.println(" response id: " + id + " for image "+ img.getName()); 
                        latch.countDown();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                } else {
                    System.out.println(" response code: " + code + " for image: " + imgFile.getName()
                            + " reason " + response.getStatusLine().getReasonPhrase());
                }
            }

            @Override
            public void failed(final Exception ex) {
                System.out.println(request.getRequestLine() + imgFile.getName() + "->" + ex);
                latch.countDown();
            }

            @Override
            public void cancelled() {
                System.out.println(request.getRequestLine() + " cancelled");
                latch.countDown();
            }

        });
        latch.await();
    }
    if (idArray.isEmpty()) {
        return null;
    } else {
        return idArray.get(0);
    }
}