阿里云视觉智能开放平台Java SDK生成URL示例

本文涉及的产品
对象存储 OSS,20GB 3个月
视觉智能开放平台,图像资源包5000点
视觉智能开放平台,视频资源包5000点
简介: 阿里云视觉智能开放平台是基于阿里巴巴视觉智能技术实践经验,面向视觉智能技术企业和开发商(含开发者),为其提供高易用、普惠的视觉API服务,帮助企业快速建立视觉智能技术应用能力的综合性视觉AI能力平台。可以为您提供普惠易用的AI能力。适用于城市大脑、安防、数字营销、互联网娱乐、手机应用等行业,企业和开发商可以选择相应能力自行封装解决方案或者是产品、服务。使用阿里云视觉智能开放平台服务时,需要传入文件URL,视觉智能平台根据传入的文件URL对文件进行智能处理。本文介绍对于非OSS用户,或者OSS所属地域不是华东2(上海)使用Java SDK通过本地文件或网络文件链接生成URL的快速调用示例。

前提:

获取到阿里云账号的AccessKey ID 和AccessKey Secret:


操作步骤:

1.添加Pom依赖

<dependency><groupId>com.aliyun</groupId><artifactId>tea-openapi</artifactId><version>0.0.7</version></dependency><dependency><groupId>com.aliyun</groupId><artifactId>openplatform20191219</artifactId><version>3.0.1</version></dependency><dependency><groupId>com.aliyun</groupId><artifactId>oss-client</artifactId><version>3.0.2</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.7</version></dependency><dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-core</artifactId><version>4.5.13</version></dependency>

2.Code  Sample

importcom.aliyun.fileform.models.FileField;
importcom.aliyun.openplatform20191219.models.AuthorizeFileUploadRequest;
importcom.aliyun.openplatform20191219.models.AuthorizeFileUploadResponse;
importcom.aliyun.oss.models.PostObjectRequest;
importcom.aliyun.tea.TeaConverter;
importcom.aliyun.tea.TeaPair;
importcom.aliyun.teautil.models.RuntimeOptions;
importorg.apache.commons.lang3.StringUtils;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.net.URL;
importjava.net.URLConnection;
importjava.net.URLDecoder;
importjava.util.HashMap;
importjava.util.Map;
importjava.util.concurrent.ConcurrentHashMap;
//  使用Java SDK通过本地文件或网络文件链接生成URLpublicclassViapiFileUtilAdvance {
publicstaticvoidmain(String[] args) throwsException {
StringaccessKey="xxxxxxxxxx";    //您的AccessKeyIDStringaccessKeySecret="xxxxxxxxxx";   //您的AccessKeySecretStringregionId="cn-shanghai";
Stringfile="D:/picture/recognize_1.jpg"; //或者本地上传//String file = "https://img.alicdn.com/tfs/TB1S0DjC4v1gK0jSZFFXXb0sXXa-692-440.jpg";try (InputStreaminputStream=buildInputStream(file)) {
ViapiFileUtilAdvancefileUtils=ViapiFileUtilAdvance.getInstance(accessKey, accessKeySecret, regionId);
StringossTempFileUrl=fileUtils.upload(inputStream);
System.out.println(ossTempFileUrl);
        }
    }
publicstaticInputStreambuildInputStream(StringfilePath) throwsIOException {
if (StringUtils.startsWithAny(filePath, newCharSequence[]{"http://", "https://"})) {
filePath=URLDecoder.decode(filePath, "UTF-8");
URLurl=newURL(filePath);
URLConnectionurlConnection=url.openConnection();
returnurlConnection.getInputStream();
        } else {
Filekey1=newFile(filePath);
returnnewFileInputStream(key1);
        }
    }
publicstaticsynchronizedViapiFileUtilAdvancegetInstance(StringaccessKeyId, StringaccessKeySecret, StringregionId) throwsException {
StringmapKey=String.format("%s:%s:%s", regionId, accessKeyId, accessKeySecret);
ViapiFileUtilAdvancefileUtils=_map.get(mapKey);
if (fileUtils==null) {
_map.putIfAbsent(mapKey, newViapiFileUtilAdvance(accessKeyId, accessKeySecret, regionId));
fileUtils=_map.get(mapKey);
        }
returnfileUtils;
    }
privateViapiFileUtilAdvance(StringaccessKeyId, StringaccessKeySecret, StringregionId) throwsException {
Map<String, Object>cm=newHashMap<>();
cm.put("regionId", regionId);
cm.put("autoretry", "true");
cm.put("accessKeyId", accessKeyId);
cm.put("accessKeySecret", accessKeySecret);
cm.put("connectTimeout", 15*1000);
cm.put("readTimeout", 30*1000);
cm.put("maxIdleConns", 200);
cm.put("type", "access_key");
cm.put("endpoint", "openplatform.aliyuncs.com");
_runtime=RuntimeOptions.build(cm);
com.aliyun.tearpc.models.ConfigauthConfig=com.aliyun.tearpc.models.Config.build(cm);
_authClient=newcom.aliyun.openplatform20191219.Client(authConfig);
//ossEndpointType = "internal";//aliyun-vpc_ossConfig=com.aliyun.oss.models.Config.build(TeaConverter.buildMap(
newTeaPair("accessKeySecret", accessKeySecret),
newTeaPair("type", "access_key"),
newTeaPair("protocol", _authClient._protocol),
newTeaPair("regionId", _authClient._regionId)
        ));
    }
publicStringupload(InputStreaminputStream) throwsException {
AuthorizeFileUploadRequestauthRequest=AuthorizeFileUploadRequest.build(TeaConverter.buildMap(
newTeaPair("product", "imageseg"),
newTeaPair("regionId", _authClient._regionId)
        ));
AuthorizeFileUploadResponseauthResponse=_authClient.authorizeFileUploadWithOptions(authRequest, _runtime);
com.aliyun.oss.models.ConfigossConfig=newcom.aliyun.oss.models.Config();
com.aliyun.openapiutil.Client.convert(_ossConfig, ossConfig);
ossConfig.accessKeyId=authResponse.accessKeyId;
ossConfig.endpoint=com.aliyun.openapiutil.Client.getEndpoint(authResponse.endpoint, authResponse.useAccelerate, ossEndpointType);
com.aliyun.oss.ClientossClient=newcom.aliyun.oss.Client(ossConfig);
FileFieldfileObj=FileField.build(TeaConverter.buildMap(
newTeaPair("filename", authResponse.objectKey),
newTeaPair("content", inputStream),
newTeaPair("contentType", "")
        ));
PostObjectRequest.PostObjectRequestHeaderossHeader=PostObjectRequest.PostObjectRequestHeader.build(TeaConverter.buildMap(
newTeaPair("accessKeyId", authResponse.accessKeyId),
newTeaPair("policy", authResponse.encodedPolicy),
newTeaPair("signature", authResponse.signature),
newTeaPair("key", authResponse.objectKey),
newTeaPair("file", fileObj),
newTeaPair("successActionStatus", "201")
        ));
PostObjectRequestuploadRequest=PostObjectRequest.build(TeaConverter.buildMap(
newTeaPair("bucketName", authResponse.bucket),
newTeaPair("header", ossHeader)
        ));
com.aliyun.ossutil.models.RuntimeOptionsossRuntime=newcom.aliyun.ossutil.models.RuntimeOptions();
com.aliyun.openapiutil.Client.convert(_runtime, ossRuntime);
ossClient.postObject(uploadRequest, ossRuntime);
StringimageURL="http://"+authResponse.bucket+"."+authResponse.endpoint+"/"+authResponse.objectKey+"";
returnimageURL;
    }
RuntimeOptions_runtime;
com.aliyun.openplatform20191219.Client_authClient;
com.aliyun.oss.models.Config_ossConfig;
StringossEndpointType=null;
staticMap<String, ViapiFileUtilAdvance>_map=newConcurrentHashMap<>();
}

3.测试结果

生成的图片地址=http://viapi-customer-pop.oss-cn-shanghai.aliyuncs.com/ce67_212614432711017986_7857979ccbdf4c3e91aeaeef84effb00


注意事项:

在添加代码块之后,在使用try内包含流会报:“try-with-resource are not supported at language level “5”错误。通过IDEA自带的alt+enter修复功能设置一个版本到7即可。

f0bc47571bade21abfbdc4101c4f798ca19743c540f8b9bdff17e84295dff8f1QzpcVXNlcnNc5a6JXEFwcERhdGFcUm9hbWluZ1xEaW5nVGFsa1wyNDU2NjQxODQ1X3YyXEltYWdlRmlsZXNcMTYzNzQ4NTQwMTg4NV8yMjM0ODhFMy05MkZGLTRmYmYtQUI1MC1BNjFDODVEOTBBQTIucG5n.png



更多参考:

衣冠の禽兽CSDN文档:IDEA 报错:not supported at language level "5"

ChaseDreamBoy  CSDN文档:idea maven 出现:Try-with-resources are not supported at language level '5'

阿里云视觉智能开放平台快速入门:生成URL







目录
相关文章
|
14天前
|
文字识别 算法 API
视觉智能开放平台产品使用合集之支持的离线SDK有哪些
视觉智能开放平台是指提供一系列基于视觉识别技术的API和服务的平台,这些服务通常包括图像识别、人脸识别、物体检测、文字识别、场景理解等。企业或开发者可以通过调用这些API,快速将视觉智能功能集成到自己的应用或服务中,而无需从零开始研发相关算法和技术。以下是一些常见的视觉智能开放平台产品及其应用场景的概览。
视觉智能开放平台产品使用合集之支持的离线SDK有哪些
|
5天前
|
存储 SQL 分布式计算
Java连接阿里云MaxCompute例
要使用Java连接阿里云MaxCompute数据库,首先需在项目中添加MaxCompute JDBC驱动依赖,推荐通过Maven管理。避免在代码中直接写入AccessKey,应使用环境变量或配置文件安全存储。示例代码展示了如何注册驱动、建立连接及执行SQL查询。建议使用RAM用户提升安全性,并根据需要配置时区和公网访问权限。具体步骤和注意事项请参考阿里云官方文档。
|
11天前
|
Java 开发工具
通过Java SDK调用阿里云模型服务
在阿里云平台上,可以通过创建应用并使用模型服务完成特定任务,如生成文章内容。本示例展示了一段简化的Java代码,演示了如何调用阿里云模型服务生成关于“春秋战国经济与文化”的简短文章。示例代码通过设置系统角色为历史学家,并提出文章生成需求,最终处理并输出生成的文章内容。在实际部署前,请确保正确配置环境变量中的密钥和ID,并根据需要调整SDK导入语句及类名。更多详情和示例,请参考相关链接。
|
13天前
|
存储 监控 API
视觉智能开放平台产品使用合集之人脸识别服务端的离线SDK是什么
视觉智能开放平台是指提供一系列基于视觉识别技术的API和服务的平台,这些服务通常包括图像识别、人脸识别、物体检测、文字识别、场景理解等。企业或开发者可以通过调用这些API,快速将视觉智能功能集成到自己的应用或服务中,而无需从零开始研发相关算法和技术。以下是一些常见的视觉智能开放平台产品及其应用场景的概览。
8 0
|
18天前
|
存储 Java 开发工具
【Azure 存储服务】Java Azure Storage SDK V12使用Endpoint连接Blob Service遇见 The Azure Storage endpoint url is malformed
【Azure 存储服务】Java Azure Storage SDK V12使用Endpoint连接Blob Service遇见 The Azure Storage endpoint url is malformed
|
21天前
|
机器学习/深度学习 存储 缓存
Java本地高性能缓存实践问题之阿里云机器学习团队开源社区的问题如何解决
Java本地高性能缓存实践问题之阿里云机器学习团队开源社区的问题如何解决
|
30天前
|
开发工具 Android开发
解决Android运行出现NDK at /Library/Android/sdk/ndk-bundle did not have a source.properties file
解决Android运行出现NDK at /Library/Android/sdk/ndk-bundle did not have a source.properties file
124 4
解决Android运行出现NDK at /Library/Android/sdk/ndk-bundle did not have a source.properties file
|
14天前
|
JavaScript 前端开发 Java
[Android][Framework]系统jar包,sdk的制作及引用
[Android][Framework]系统jar包,sdk的制作及引用
31 0
|
1月前
|
Dart 开发工具 Android开发
Android Studio导入Flutter项目提示Dart SDK is not configured
Android Studio导入Flutter项目提示Dart SDK is not configured
65 4
|
1月前
|
开发工具 Android开发
Flutter: Android SDK not found at this location,Android Studio not found at xxx
Flutter: Android SDK not found at this location,Android Studio not found at xxx
104 2