我正在尝试在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'.
任何帮助将不胜感激。谢谢。
答案 0 :(得分:2)
我建议您使用一个简单的服务器端Java应用程序检查您上传到Cloudinary的内容。像这样:-
G27
上面的示例一旦运行,您就可以继续测试字节上传了,这将确保您没有配置问题。
这里是一个例子。我正在使用apache文件上传:
public Mono<PaymentResponse> execute(PaymentTransaction transaction, WebClient client) {
long conn = 1L;
int sec = 1232;
Mono<PaymentTransaction> transactionMono = Mono.just(transaction);
return client.post()
.uri(uriBuilder -> uriBuilder.scheme("https").host("www.test.com")
.path("notification")
.queryParam("con", conn)
.queryParam("sec", sec)
.build())
.accept(MediaType.APPLICATION_XML)
.contentType(MediaType.APPLICATION_XML)
.body(transactionMono, PaymentTransaction.class)
.retrieve()
.bodyToMono(PaymentResponse.class);
}
答案 1 :(得分:1)
我设法使其正常运行。我最终将上传的文件内容从primefaces uploadFile复制到临时文件,然后将该文件发送到Cloudinary上传。
托管的Bean类方法(上传处理程序):
my.function
Cloudinary上传方法:
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);
}
}
谢谢。