1 Reply Latest reply on Jan 19, 2015 1:09 PM by jknotzke

    Uploaded image not recognized as an image

    jknotzke

         Hi

       

        In java and using Apache's HTTP Client, I am able to upload an image. When I do, I get the following:

       

      {
        "id" : "18388",
        "contentType" : "application/octet-stream",
        "name" : "some_image.jpg",
        "ref" : "https://example-preview.jiveon.com/api/core/v3/images/18388?a=1421693489937",
        "size" : 140306,
        "width" : 800,
        "height" : 800,
        "type" : "image"
      }
      
      

      However, when I hit that URL, my browser downloads the file as oppose to displaying it. Jive does not seem to know that this file is in fact an image..

       

      Doing the same thing using CURL however, the same image is displayed in the browser and not downloaded.. So I suspect there is something wrong in my code. They key difference is that with the CURL upload the "contentType" is set to "image/jpeg"

       

      {
        "id" : "18387",
        "contentType" : "image/jpeg",
        "name" : "my_cat.jpg",
        "ref" : "https://example-preview.jiveon.com/api/core/v3/images/18387?a=1421693341520",
        "size" : 86012,
        "width" : 630,
        "height" : 778,
        "type" : "image"
      }
      
      

      Would anyone have any examples or any idea as to what I am doing wrong ? Here is the code in question:

       

      public String postImageToJive(String imagePath) {
      
      
        String uploadedURL = "";
        HttpHost target = new HttpHost("example-preview.jiveon.com",
        443, "https");
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(
        new AuthScope(target.getHostName(), target.getPort()),
        new UsernamePasswordCredentials("import@example.com",
        "foo"));
      
      
        CloseableHttpClient httpclient = HttpClients.custom()
        .setDefaultCredentialsProvider(credsProvider).build();
        // Create AuthCache instance
        try {
      
      
        AuthCache authCache = new BasicAuthCache();
        // Generate BASIC scheme object and add it to the local
        // auth cache
        BasicScheme basicAuth = new BasicScheme();
        authCache.put(target, basicAuth);
      
      
        // Add AuthCache to the execution context
        HttpClientContext localContext = HttpClientContext.create();
        localContext.setAuthCache(authCache);
        HttpPost httppost = new HttpPost(
        "https://example-preview.jiveon.com/api/core/v3/images");
        //httppost.addHeader("content-type", "image/jpg");
      
      
        FileBody bin = new FileBody(new File(imagePath)); // image for
      
      
        MultipartEntityBuilder multipartEntity  = MultipartEntityBuilder.create();       
      
      
        /* example for setting a HttpMultipartMode */
        multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        multipartEntity.addPart("my_file", bin);
        multipartEntity.addTextBody("type", "image/jpg");
      
      
        HttpEntity reqEntity = multipartEntity.build();
        httppost.setEntity(reqEntity);
      
      
        System.out
        .println("executing request " + httppost.getRequestLine());
        CloseableHttpResponse response = httpclient.execute(target,
        httppost, localContext);
      
      
        try {
        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());
        HttpEntity entity = response.getEntity();
        //System.out.println(EntityUtils.toString(entity));
        String tmp = EntityUtils.toString(entity);
        JSONObject jsonObject = new JSONObject(tmp);
        System.out.println("REF: " + jsonObject.get("ref"));
                      uploadedURL = (String)jsonObject.get("ref");
      
        EntityUtils.consume(response.getEntity());
      
      
        } finally {
        response.close();
        }
        } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        } finally {
        try {
        httpclient.close();
        } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
        }
      
        return uploadedURL;
        }
      
      

       

       

      Thanks

       

      J

        • Re: Uploaded image not recognized as an image
          jknotzke

          Hi

           

          I figured it out.. Here is the code that works:

           

          The key was line 39.

           

          J

          public String postImageToJive(String imagePath) {
          
          
            String uploadedURL = "";
            HttpHost target = new HttpHost("example-preview.jiveon.com",
            443, "https");
            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            credsProvider.setCredentials(
            new AuthScope(target.getHostName(), target.getPort()),
            new UsernamePasswordCredentials("import@example.com",
            "foo"));
          
            CloseableHttpClient httpclient = HttpClients.custom()
            .setDefaultCredentialsProvider(credsProvider).build();
            // Create AuthCache instance
            try {
          
            AuthCache authCache = new BasicAuthCache();
            // Generate BASIC scheme object and add it to the local
            // auth cache
            BasicScheme basicAuth = new BasicScheme();
            authCache.put(target, basicAuth);
          
            // Add AuthCache to the execution context
            HttpClientContext localContext = HttpClientContext.create();
            localContext.setAuthCache(authCache);
            HttpPost httppost = new HttpPost(
            "https://example-preview.jiveon.com/api/core/v3/images");
            httppost.addHeader("contentType", "image/jpeg");
          
            File imageFile = new File(imagePath);
            //FileBody bin = new FileBody(imageFile));
          
            MultipartEntityBuilder multipartEntity  = MultipartEntityBuilder.create();     
          
            /* example for setting a HttpMultipartMode */
            multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
            //multipartEntity.addPart("my_file", bin);
            multipartEntity.addBinaryBody("someName", imageFile, ContentType.create("image/jpeg"), imageFile.getName());
          
            HttpEntity reqEntity = multipartEntity.build();
            httppost.setEntity(reqEntity);
          
          
            System.out
            .println("executing request " + httppost.getRequestLine());
            CloseableHttpResponse response = httpclient.execute(target,
            httppost, localContext);
          
          
            try {
            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            HttpEntity entity = response.getEntity();
            String tmp = EntityUtils.toString(entity);
            System.out.println(tmp);
            JSONObject jsonObject = new JSONObject(tmp);
            System.out.println("REF: " + jsonObject.get("ref"));
                          uploadedURL = (String)jsonObject.get("ref");
          
            EntityUtils.consume(response.getEntity());
          
          
            } finally {
            response.close();
            }
            } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            } finally {
            try {
            httpclient.close();
            } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }
            }
          
            return uploadedURL;
            }