如何在java中生成签名并在jQuery(Cloudinary)中使用它?

我正在尝试使用jQuery和Java将图像上传到Cloudinary。 我试过这个链接的代码没有运气。 我在生成签名时遇到错误。 有没有人有一个生成签名的工作实现的例子? 那会更有帮助。

将以下代码放在index.jsp中:

              

如果你有一个maven项目,那么将这些依赖项放在你的pom.xml中,或者下载它们的jar并将它们放在你的’lib’文件夹中。

  org.json json 20090211   commons-codec commons-codec 1.4  

最后创建一个Servlet:

 import org.apache.commons.codec.digest.DigestUtils; import org.json.JSONObject; public class SignRequestServlet extends HttpServlet{ public void doGet(HttpServletRequet request, HttpServletResponse response) throws ServletException, IOException{ long unixTime = System.currentTimeMillis() / 1000L; String unsignedData = ""api_key":"874837483274837","timestamp":"" + unixTime + """; String unsignedDataForSHA = "timestamp="+unixTime; String apiSecret = "-----place your api secret here-----"; String unsignedDataSecret = unsignedDataForSHA + apiSecret; String signedData = unsignedData + ","signature":"" + DigestUtils.shaHex(unsignedDataSecret.getBytes()) + "&quot"; JSONObject jsonObject = new JSONObject("{" + signedData.replaceAll(""", "\"") + "}"); request.setAttribute("signedData", jsonObject.toString()); request.getRequestDispatcher("/index.jsp").forward(request,response); } public void doPost(HttpServletRequet request, HttpServletResponse response) throws ServletException, IOException{ doGet(request, response); } } 

请记住:替换Cloudinary控制台主页中给出的api-key,cloud-name和api-secret。 此外,您可能需要更改index.jsp中.js文件的位置。

虽然非常感谢Asim的回答,但也请注意Cloudinary的Java SDK已经提供了一种帮助方法来为您生成签名。 简单地通过params,你就完成了。 请参阅以下内容以供参考: https : //github.com/cloudinary/cloudinary_java/blob/master/cloudinary-core/src/main/java/com/cloudinary/Cloudinary.java#L133