Can't upload image in JSF application

Comments

6 comments

  • Avatar
    Aditi Madan

    Once you have configured Coudinary with your java application , you should be able to upload the images without having the need to generate the signature on the back-end.

    A simple upload in JAVA with Cloudinary:-

    Map upload=cloudinary.uploader().upload("/Users/Downloads/test.jpg", ObjectUtils.emptyMap());System.out.println(upload);

    Can you please check your configuration and share your upload code if you continue seeing the issue.

    Thanks,
    Aditi

     

    0
    Comment actions Permalink
  • Avatar
    Cvetan Simsic

    What part of configuration? API key, API secret, etc?

    If that's what you meant, that part is configured properly. As i said, i've tried with emty map, but doesn't work nevertheless.

    By the way it is JSF web application. And i've tried instanting with byte array since it is uploaded on page, doesn't work. I've tried to upload image from disk, doesn't work. As i wrote Exception thrown, says something about timestamp.

    This is Managed bean action that triggers upload.

    public String upload() {
        try {
            String filename = CloudinaryFacade.upload(file.getContents());
                
            return Redirector.redirectWithMessage(filename, FacesMessage.SEVERITY_INFO, null);
        } catch (IOException ex) {
                
            return Redirector.redirectWithMessage(ex.getMessage(), FacesMessage.SEVERITY_ERROR, null);
        }

    }

    This is class that performs upload through Cloduinary.

    package com.github.cvetan.bookstore.util;

    import com.cloudinary.*;
    import com.cloudinary.utils.ObjectUtils;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;

    /**
     *
     * @author cvetan
     */
    public class CloudinaryFacade {
        
        private final static Map<Object, Object> CONFIG = new HashMap<>();
        
        static {
            CONFIG.put("cloud_name", "cvetan");
            CONFIG.put("api_key", "***");
            CONFIG.put("api_secret", "***");
        }
        
        public static String upload(byte[] file) throws IOException {
            Cloudinary cloudinary = new Cloudinary(CONFIG);
            
            Map result = cloudinary.uploader().upload(file, ObjectUtils.emptyMap());
            
            return (String) result.get("url");
        }
    }
    0
    Comment actions Permalink
  • Avatar
    Aditi Madan

    I will recommend checking your upload to Cloudinary with a simple server-side java app. Something like this:-

    import com.cloudinary.*;
    import com.cloudinary.utils.ObjectUtils;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;
    
    /**
    
    *
    
    * @author cvetan
    
    *
    
    */
    
    public class CloudinaryFacade {
          private final static Map<Object, Object> CONFIG = new HashMap<>();
    
            static {
                CONFIG.put("cloud_name", "");
                CONFIG.put("api_key", "");
                CONFIG.put("api_secret", "");
            }
    
          public static void main(String[] args) throws IOException {
               Cloudinary cloudinary = new Cloudinary(CONFIG);
    
                Map result = cloudinary.uploader().upload("https://res.cloudinary.com/demo/image/upload/sample.jpg", ObjectUtils.emptyMap());
    
                System.out.println(result);
        }
    
    }
    

    Once the above example is working you can move on testing the byte upload. This will ensure you don't have config issues.

    Here is an example. I am using apache file upload:

    List<FileItem> formItems = upload.parseRequest(request);//List of file items
    for (FileItem item : formItems) {
        String fileName = item.getName();
        //save on Cloudinary
        Map imageUpload=cloudinary.uploader().upload(item.get(), 
        ObjectUtils.asMap("public_id",fileName));
    }
    0
    Comment actions Permalink
  • Avatar
    Cvetan Simsic

    It don't think it is configuration error, since i got another error, when i entered wrong api key.

    But i managed to upload it with your example.

    And i don't think it is related to byte array upload either, since i am getting some timestamp error, and i got the same when i tried upload with File, from path on the disk.

    So what could be issue with web application?

    This was response.

    {

        signature=2f6867bbc833cbcad5246a67297459c74915acd1,
        format=jpg,
        resource_type=image,
        secure_url=https://res.cloudinary.com/cvetan/image/upload/v1533812401/lrkm4vgsltrghchri9jt.jpg,
        created_at=2018-08-09T11:00:01Z,
        type=upload,
        version=1533812401,
        url=http://res.cloudinary.com/cvetan/image/upload/v1533812401/lrkm4vgsltrghchri9jt.jpg,
        public_id=lrkm4vgsltrghchri9jt,
        tags=[],
        original_filename=sample,
        bytes=120253,
        width=864,
        etag=83340520d28b704ca4f4b019effb33dc,
        placeholder=false,
        height=576
    }
    0
    Comment actions Permalink
  • Avatar
    Aditi Madan

    I see you have solved the issue: https://support.cloudinary.com/hc/en-us/community/posts/360018958051-Can-t-upload-image-in-JSF-application

    Please let me know if you have any other questions.

    Thanks,

    Aditi

    0
    Comment actions Permalink
  • Avatar
    Cvetan Simsic

    Yes, i did. I will wirte the solution here if anyone else needs it. Thank you very much.

    So I ended up copying the uploaded file contents from primefaces uploadedFile into temporary file, and sending that file to Cloudinary upload.

     

    Managed bean class method:

    public String upload() {
            try {
                File uploadedFile = File.createTempFile("image", ".tmp");
    
                InputStream content = file.getInputstream();
    
                Files.copy(content, uploadedFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
    
                String filename = CloudinaryFacade.upload(uploadedFile);
    
                return Redirector.redirectWithMessage(filename, FacesMessage.SEVERITY_INFO, null);
            } catch (IOException ex) {
                return Redirector.redirectWithMessage(ex.getMessage(), FacesMessage.SEVERITY_ERROR, null);
            }
        }

    Cloudinary upload method:

    public static String upload(File file) throws IOException {
            Cloudinary cloudinary = new Cloudinary(CONFIG);
            Map<Object, Object> parameters = new HashMap<>();
            parameters.put("public_id", "Bookstore/Authors/Images/vejder");
    
            Map result = cloudinary.uploader().upload(file, parameters);
    
            return (String) result.get("url");
        }
    0
    Comment actions Permalink

Please sign in to leave a comment.