【问题标题】:How to generate signature in java and use it in jQuery (Cloudinary)?如何在 java 中生成签名并在 jQuery (Cloudinary) 中使用它?
【发布时间】:2015-12-25 08:56:15
【问题描述】:

我正在尝试使用 jQuery 和 Java 将图像上传到 Cloudinary。我尝试了link 中的代码,但没有成功。我在生成签名时遇到错误。 有没有人有一个生成签名的工作实现示例?那会更有帮助。

【问题讨论】:

  • 先生,现在我成功上传和检索图像。我的问题是如何在 jquery 中删除上传的图像?。

标签: java jquery cloudinary


【解决方案1】:

将以下代码放入您的 index.jsp:

<html>
        <head>
            <title></title>
            <script src='jquery.min.js' type='text/javascript'></script>
            <script src='jquery.ui.widget.js' type='text/javascript'></script>
            <script src='jquery.iframe-transport.js' type='text/javascript'></script>
            <script src='jquery.fileupload.js' type='text/javascript'></script>
            <script src='jquery.cloudinary.js' type='text/javascript'></script>
            <script type = "text/javascript">
                $.cloudinary.config({ cloud_name: 'sample', api_key: '874837483274837'});
            </script>
        </head>
        <body>
            <input name="file" type="file" 
               class="cloudinary-fileupload" data-cloudinary-field="image_id" 
               data-form-data="${signedData}" />
        </body>
    </html>

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

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20090211</version>
</dependency>
<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.4</version>
</dependency>

最后创建一个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 = "&quot;api_key&quot;:&quot;874837483274837&quot;,&quot;timestamp&quot;:&quot;" + unixTime + "&quot;";
        String unsignedDataForSHA = "timestamp="+unixTime;
        String apiSecret = "-----place your api secret here-----";
        String unsignedDataSecret = unsignedDataForSHA + apiSecret;
        String signedData = unsignedData + ",&quot;signature&quot;:&quot;" + DigestUtils.shaHex(unsignedDataSecret.getBytes()) + "&quot";

        JSONObject jsonObject = new JSONObject("{" + signedData.replaceAll("&quot;", "\"") + "}");

        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、c​​loud-name 和 api-secret。此外,您可能需要更改 index.jsp 中 .js 文件的位置。

【讨论】:

  • 感谢阿西姆,帮了大忙
  • 你编写了我的代码,我刚刚添加了: var codeSuccess = function(response) { var code = response.o $("#xxx").attr('data-form-data' , 代码) } $.getJSON(GET_CODE, codeSuccess)
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2017-02-05
    • 2019-11-03
    • 2017-02-25
    • 2016-06-29
    • 1970-01-01
    • 2022-08-16
    • 1970-01-01
    • 1970-01-01
    • 2020-01-27
    相关资源
    最近更新 更多