简体   繁体   English

如何使用java将图像上传到cloudinary中具有唯一public_id的特定文件夹?

[英]How to upload images to specific folder with unique public_id in cloudinary using java?

I have configured my project to upload an images to Cloudinary (Cloud storage for images).我已将我的项目配置为将图像上传到Cloudinary (图像的云存储)。 In my cloudinary account i have created a folder of my application name say MyAppImages .在我的 cloudinary 帐户中,我创建了一个以我的应用程序名称命名的文件夹,例如MyAppImages Now my requirement is that my uploaded images through MyApp application should go to MyAppImages folder.现在我的要求是我通过MyApp应用程序上传的图像应该转到MyAppImages文件夹。

With my current code it is going directly to Samples folder.使用我当前的代码,它将直接转到 Samples 文件夹。

My Code:我的代码:

{
String fileName=multipartFile.getOriginalFilename();
            File physicalFile=new File(multipartFile.getOriginalFilename());
            FileOutputStream fout=new FileOutputStream(fileDir.getName()+"/"+physicalFile);
            fout.write(multipartFile.getBytes());
            fout.close();
            Cloudinary cloudinary = new Cloudinary(ObjectUtils.asMap(
                      "cloud_name", "my_cloud_name",
                      "api_key", "my_key",
                      "api_secret", "my_secret"));

            File toUpload = new File("rowFiles/"+fileName);
            Map params = ObjectUtils.asMap("public_id", "MyAppImages/"+fileName);
            Map uploadResult = cloudinary.uploader().upload(toUpload, params );
            System.out.println("==============>>"+uploadResult.get("url"));
            cloudinaryImgURL=uploadResult.get("url").toString();
}

with above code i am able to upload image to specific directory MyAppImages , but with exactly name with original image name.使用上面的代码,我可以将图像上传到特定目录MyAppImages ,但名称与原始图像名称完全相同。 I want to store it image by cloudinary generated random name, so that if i upload the same image twice it should not override first image with second uploaded image as is happening with above code.我想通过cloudinary生成的随机名称存储它的图像,这样如果我上传相同的图像两次,它不应该像上面的代码那样用第二个上传的图像覆盖第一张图像。

By default, when using Cloudinary's API for uploading, Cloudinary will set a randomly generated public ID for the uploaded image.默认情况下,使用 Cloudinary 的 API 上传时,Cloudinary 会为上传的图片设置一个随机生成的公共 ID。

You can set the use_filename parameter to true to set the uploaded image's public ID to be as the original file's name.您可以将use_filename 参数设置为true,将上传图片的公共ID 设置为原始文件的名称。 However, a random string is appended to the image's public ID to ensure uniqueness.但是,随机字符串附加到图像的公共 ID 以确保唯一性。 For more information: http://support.cloudinary.com/entries/26977753-Why-does-the-public-ID-include-additional-characters-appended-to-the-original-file-name-有关更多信息: http : //support.cloudinary.com/entries/26977753-Why-does-the-public-ID-include-additional-characters-appended-to-the-original-file-name-

To keep the original filename of the file you uploaded, you can set the use_filename parameter to true and Cloudinary will use the file name of the uploaded image for the public ID.为了保留上传文件的原始文件名,您可以将 use_filename 参数设置为 true,Cloudinary 将使用上传图像的文件名作为公共 ID。 The file name is normalized and random characters are appended to ensure uniqueness.文件名被标准化并附加随机字符以确保唯一性。

To skip adding random characters that ensure uniqueness, you can additionally specify the unique_filename parameter, with the value set to false要跳过添加确保唯一性的随机字符,您可以另外指定 unique_filename 参数,并将值设置为 false

Source : https://support.cloudinary.com/hc/en-us/articles/202520752-Can-I-upload-files-to-a-folder-while-keeping-their-original-file-names-来源: https : //support.cloudinary.com/hc/en-us/articles/202520752-Can-I-upload-files-to-a-folder-while-keeping-their-original-file-names-

For more information on the upload API call parameters: https://cloudinary.com/documentation/image_upload_api_reference#required_parameters有关上传 API 调用参数的更多信息: https : //cloudinary.com/documentation/image_upload_api_reference#required_pa​​rameters

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM