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

简介: 阿里云视觉智能开放平台是基于阿里巴巴视觉智能技术实践经验,面向视觉智能技术企业和开发商(含开发者),为其提供高易用、普惠的视觉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







目录
相关文章
|
机器学习/深度学习 人工智能 NoSQL
JAVA接入DeepSeek大模型接口开发---阿里云的百炼模型
随着大模型的越来越盛行,现在很多企业开始接入大模型的接口,今天我从java开发角度来写一个demo的示例,用于接入DeepSeek大模型,国内的大模型有很多的接入渠道,今天主要介绍下阿里云的百炼模型,因为这个模型是免费的,只要注册一个账户,就会免费送百万的token进行学习,今天就从一个简单的可以执行的示例开始进行介绍,希望可以分享给各位正在学习的同学们。
2774 3
JAVA接入DeepSeek大模型接口开发---阿里云的百炼模型
|
Arthas 监控 Java
拥抱 OpenTelemetry:阿里云 Java Agent 演进实践
拥抱 OpenTelemetry:阿里云 Java Agent 演进实践
870 0
|
Arthas 监控 Java
拥抱 OpenTelemetry:阿里云 Java Agent 演进实践
本文介绍了阿里云 Java Agent 4.x 版本在基于 OTel Java Agent 二次开发过程中的实践与思考,并重点从功能、性能、稳定性、兼容性四个方面介绍了所做的工作。同时也介绍了阿里云可观测团队积极参与开源建设取得的丰厚成果。
1782 136
拥抱 OpenTelemetry:阿里云 Java Agent 演进实践
|
存储 SQL 分布式计算
Java连接阿里云MaxCompute例
要使用Java连接阿里云MaxCompute数据库,首先需在项目中添加MaxCompute JDBC驱动依赖,推荐通过Maven管理。避免在代码中直接写入AccessKey,应使用环境变量或配置文件安全存储。示例代码展示了如何注册驱动、建立连接及执行SQL查询。建议使用RAM用户提升安全性,并根据需要配置时区和公网访问权限。具体步骤和注意事项请参考阿里云官方文档。
1393 10
|
JSON 网络协议 安全
【Java】(10)进程与线程的关系、Tread类;讲解基本线程安全、网络编程内容;JSON序列化与反序列化
几乎所有的操作系统都支持进程的概念,进程是处于运行过程中的程序,并且具有一定的独立功能,进程是系统进行资源分配和调度的一个独立单位一般而言,进程包含如下三个特征。独立性动态性并发性。
770 1
|
JSON 网络协议 安全
【Java基础】(1)进程与线程的关系、Tread类;讲解基本线程安全、网络编程内容;JSON序列化与反序列化
几乎所有的操作系统都支持进程的概念,进程是处于运行过程中的程序,并且具有一定的独立功能,进程是系统进行资源分配和调度的一个独立单位一般而言,进程包含如下三个特征。独立性动态性并发性。
685 1
|
数据采集 存储 弹性计算
高并发Java爬虫的瓶颈分析与动态线程优化方案
高并发Java爬虫的瓶颈分析与动态线程优化方案
Java 数据库 Spring
567 0
|
算法 Java
Java多线程编程:实现线程间数据共享机制
以上就是Java中几种主要处理多线程序列化资源以及协调各自独立运行但需相互配合以完成任务threads 的技术手段与策略。正确应用上述技术将大大增强你程序稳定性与效率同时也降低bug出现率因此深刻理解每项技术背后理论至关重要.
817 16
|
缓存 并行计算 安全
关于Java多线程详解
本文深入讲解Java多线程编程,涵盖基础概念、线程创建与管理、同步机制、并发工具类、线程池、线程安全集合、实战案例及常见问题解决方案,助你掌握高性能并发编程技巧,应对多线程开发中的挑战。
1003 0

热门文章

最新文章