如何在Java中生成签名并在jQuery(Cloudinary)中使用它? [英] How to generate signature in java and use it in jQuery (Cloudinary)?

查看:107
本文介绍了如何在Java中生成签名并在jQuery(Cloudinary)中使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jQuery和Java将图像上传到Cloudinary.我在页面中尝试了代码链接,没有运气.我在生成签名时遇到错误. 有人有生成签名的有效实现示例吗?那会更有帮助.

I'm trying to upload images to Cloudinary using jQuery and Java. I tried the code in this link with no luck. I'm getting errors in generating the signature. Does anyone have an example of a working implementation for generating the signature? That would be more helpful.

推荐答案

将以下代码放入index.jsp:

Place the following code in your 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"文件夹中.

If you have a maven project then place these dependencies in your pom.xml or else download their jars and place them in your 'lib' folder.

<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,如下所示:

Finally create a Servlet as:

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,cloud-name和api-secret进行替换.另外,您可能需要更改index.jsp中.js文件的位置.

Remember: Replace the api-key, cloud-name and api-secret as given in the home page of Cloudinary console. Also, you may need to change the location of the .js files in index.jsp.

这篇关于如何在Java中生成签名并在jQuery(Cloudinary)中使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆