猿问

为什么我在 Cloudinary Java 上收到“无效签名”错误?

我正在尝试在 JSF 应用程序中实现 Cloudinary 上传。根据 Cloudinary 网站上的说明,我正在使用此依赖项:


<dependency>

    <groupId>com.cloudinary</groupId>

    <artifactId>cloudinary-http44</artifactId>

    <version>1.19.0</version>

</dependency>

我有一个用于上传的类:


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");

    }

}

但是当我尝试时,会抛出下面的异常:


Invalid Signature 6e527a754f1f6fd84df0bd4c092df881c0ddc65f. String to sign - 'timestamp=1533653472'.

任何帮助将不胜感激。谢谢。


白板的微信
浏览 180回答 2
2回答

当年话下

我已经设法让它工作。我最终将上传的文件内容从 PrimeFaces 复制uploadedFile到一个临时文件中,并将该文件发送到 Cloudinary 上传。托管 bean 类方法(上传处理程序):public String upload() {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; File uploadedFile = File.createTempFile("image", ".tmp");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InputStream content = file.getInputstream();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Files.copy(content, uploadedFile.toPath(), StandardCopyOption.REPLACE_EXISTING);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String filename = CloudinaryFacade.upload(uploadedFile);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Redirector.redirectWithMessage(filename, FacesMessage.SEVERITY_INFO, null);&nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException ex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Redirector.redirectWithMessage(ex.getMessage(), FacesMessage.SEVERITY_ERROR, null);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }云端上传方式:public static String upload(File file) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; Cloudinary cloudinary = new Cloudinary(CONFIG);&nbsp; &nbsp; &nbsp; &nbsp; Map<Object, Object> parameters = new HashMap<>();&nbsp; &nbsp; &nbsp; &nbsp; parameters.put("public_id", "Bookstore/Authors/Images/vejder");&nbsp; &nbsp; &nbsp; &nbsp; Map result = cloudinary.uploader().upload(file, parameters);&nbsp; &nbsp; &nbsp; &nbsp; return (String) result.get("url");&nbsp; &nbsp; }谢谢你。
随时随地看视频慕课网APP

相关分类

Java
我要回答