作者:俏巴
概述
图像识别服务(Image Recognition)基于大数据和深度学习实现,可精准识别图像中的视觉内容,包括上千种物体标签、数十种常见场景等,包含场景分类、图像打标、鉴黄等在线API服务模块,应用于智能相册管理、图片分类和检索、图片安全监控等场景。下面给出使用Java语言分别使用网络图片和本地图片调用场景识别API的示例。
控制台测试
与人脸识别类似,可以参考:阿里云人脸识别使用流程简介 。
Java Code Sample
使用网络图片测试
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Encoder;
import javax.crypto.Mac;
//@SuppressWarnings("restriction")
public class ComputerDemo1 {
<span class="hljs-comment">/*
* 计算MD5+BASE64
*/</span>
public <span class="hljs-keyword">static</span> <span class="hljs-built_in">String</span> MD5Base64(<span class="hljs-built_in">String</span> s) {
<span class="hljs-keyword">if</span> (s == <span class="hljs-literal">null</span>)
<span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;
<span class="hljs-built_in">String</span> encodeStr = <span class="hljs-string">""</span>;
byte[] utfBytes = s.getBytes();
MessageDigest mdTemp;
<span class="hljs-keyword">try</span> {
mdTemp = MessageDigest.getInstance(<span class="hljs-string">"MD5"</span>);
mdTemp.update(utfBytes);
byte[] md5Bytes = mdTemp.digest();
BASE64Encoder b64Encoder = <span class="hljs-keyword">new</span> BASE64Encoder();
encodeStr = b64Encoder.encode(md5Bytes);
} <span class="hljs-keyword">catch</span> (Exception e) {
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">"Failed to generate MD5 : "</span> + e.getMessage());
}
<span class="hljs-keyword">return</span> encodeStr;
}
<span class="hljs-comment">/*
* 计算 HMAC-SHA1
*/</span>
public <span class="hljs-keyword">static</span> <span class="hljs-built_in">String</span> HMACSha1(<span class="hljs-built_in">String</span> data, <span class="hljs-built_in">String</span> key) {
<span class="hljs-built_in">String</span> result;
<span class="hljs-keyword">try</span> {
SecretKeySpec signingKey = <span class="hljs-keyword">new</span> SecretKeySpec(key.getBytes(), <span class="hljs-string">"HmacSHA1"</span>);
Mac mac = Mac.getInstance(<span class="hljs-string">"HmacSHA1"</span>);
mac.init(signingKey);
byte[] rawHmac = mac.doFinal(data.getBytes());
result = (<span class="hljs-keyword">new</span> BASE64Encoder()).encode(rawHmac);
} <span class="hljs-keyword">catch</span> (Exception e) {
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">"Failed to generate HMAC : "</span> + e.getMessage());
}
<span class="hljs-keyword">return</span> result;
}
<span class="hljs-comment">/*
* 等同于javaScript中的 new Date().toUTCString();
*/</span>
public <span class="hljs-keyword">static</span> <span class="hljs-built_in">String</span> toGMTString(<span class="hljs-built_in">Date</span> date) {
SimpleDateFormat df = <span class="hljs-keyword">new</span> SimpleDateFormat(<span class="hljs-string">"E, dd MMM yyyy HH:mm:ss z"</span>, Locale.UK);
df.setTimeZone(<span class="hljs-keyword">new</span> java.util.SimpleTimeZone(<span class="hljs-number">0</span>, <span class="hljs-string">"GMT"</span>));
<span class="hljs-keyword">return</span> df.format(date);
}
<span class="hljs-comment">/*
* 发送POST请求
*/</span>
public <span class="hljs-keyword">static</span> <span class="hljs-built_in">String</span> sendPost(<span class="hljs-built_in">String</span> url, <span class="hljs-built_in">String</span> body, <span class="hljs-built_in">String</span> ak_id, <span class="hljs-built_in">String</span> ak_secret) throws Exception {
PrintWriter out = <span class="hljs-literal">null</span>;
BufferedReader <span class="hljs-keyword">in</span> = <span class="hljs-literal">null</span>;
<span class="hljs-built_in">String</span> result = <span class="hljs-string">""</span>;
int statusCode = <span class="hljs-number">200</span>;
<span class="hljs-keyword">try</span> {
URL realUrl = <span class="hljs-keyword">new</span> URL(url);
<span class="hljs-comment">/*
* http header 参数
*/</span>
<span class="hljs-built_in">String</span> method = <span class="hljs-string">"POST"</span>;
<span class="hljs-built_in">String</span> accept = <span class="hljs-string">"application/json"</span>;
<span class="hljs-built_in">String</span> content_type = <span class="hljs-string">"application/json"</span>;
<span class="hljs-built_in">String</span> path = realUrl.getFile();
<span class="hljs-built_in">String</span> date = toGMTString(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>());
<span class="hljs-comment">// 1.对body做MD5+BASE64加密</span>
<span class="hljs-built_in">String</span> bodyMd5 = MD5Base64(body);
<span class="hljs-built_in">String</span> stringToSign = method + <span class="hljs-string">"\n"</span> + accept + <span class="hljs-string">"\n"</span> + bodyMd5 + <span class="hljs-string">"\n"</span> + content_type + <span class="hljs-string">"\n"</span> + date + <span class="hljs-string">"\n"</span>
+ path;
<span class="hljs-comment">// 2.计算 HMAC-SHA1</span>
<span class="hljs-built_in">String</span> signature = HMACSha1(stringToSign, ak_secret);
<span class="hljs-comment">// 3.得到 authorization header</span>
<span class="hljs-built_in">String</span> authHeader = <span class="hljs-string">"Dataplus "</span> + ak_id + <span class="hljs-string">":"</span> + signature;
<span class="hljs-comment">// 打开和URL之间的连接</span>
URLConnection conn = realUrl.openConnection();
<span class="hljs-comment">// 设置通用的请求属性</span>
conn.setRequestProperty(<span class="hljs-string">"accept"</span>, accept);
conn.setRequestProperty(<span class="hljs-string">"content-type"</span>, content_type);
conn.setRequestProperty(<span class="hljs-string">"date"</span>, date);
conn.setRequestProperty(<span class="hljs-string">"Authorization"</span>, authHeader);
<span class="hljs-comment">// 发送POST请求必须设置如下两行</span>
conn.setDoOutput(<span class="hljs-literal">true</span>);
conn.setDoInput(<span class="hljs-literal">true</span>);
<span class="hljs-comment">// 获取URLConnection对象对应的输出流</span>
out = <span class="hljs-keyword">new</span> PrintWriter(conn.getOutputStream());
<span class="hljs-comment">// 发送请求参数</span>
out.print(body);
<span class="hljs-comment">// flush输出流的缓冲</span>
out.flush();
<span class="hljs-comment">// 定义BufferedReader输入流来读取URL的响应</span>
statusCode = ((HttpURLConnection)conn).getResponseCode();
<span class="hljs-keyword">if</span>(statusCode != <span class="hljs-number">200</span>) {
<span class="hljs-keyword">in</span> = <span class="hljs-keyword">new</span> BufferedReader(<span class="hljs-keyword">new</span> InputStreamReader(((HttpURLConnection)conn).getErrorStream()));
} <span class="hljs-keyword">else</span> {
<span class="hljs-keyword">in</span> = <span class="hljs-keyword">new</span> BufferedReader(<span class="hljs-keyword">new</span> InputStreamReader(conn.getInputStream()));
}
<span class="hljs-built_in">String</span> line;
<span class="hljs-keyword">while</span> ((line = <span class="hljs-keyword">in</span>.readLine()) != <span class="hljs-literal">null</span>) {
result += line;
}
} <span class="hljs-keyword">catch</span> (Exception e) {
e.printStackTrace();
} <span class="hljs-keyword">finally</span> {
<span class="hljs-keyword">try</span> {
<span class="hljs-keyword">if</span> (out != <span class="hljs-literal">null</span>) {
out.close();
}
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">in</span> != <span class="hljs-literal">null</span>) {
<span class="hljs-keyword">in</span>.close();
}
} <span class="hljs-keyword">catch</span> (IOException ex) {
ex.printStackTrace();
}
}
<span class="hljs-keyword">if</span> (statusCode != <span class="hljs-number">200</span>) {
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> IOException(<span class="hljs-string">"\nHttp StatusCode: "</span>+ statusCode + <span class="hljs-string">"\nErrorMessage: "</span> + result);
}
<span class="hljs-keyword">return</span> result;
}
<span class="hljs-comment">/*
* GET请求
*/</span>
public <span class="hljs-keyword">static</span> <span class="hljs-built_in">String</span> sendGet(<span class="hljs-built_in">String</span> url, <span class="hljs-built_in">String</span> ak_id, <span class="hljs-built_in">String</span> ak_secret) throws Exception {
<span class="hljs-built_in">String</span> result = <span class="hljs-string">""</span>;
BufferedReader <span class="hljs-keyword">in</span> = <span class="hljs-literal">null</span>;
int statusCode = <span class="hljs-number">200</span>;
<span class="hljs-keyword">try</span> {
URL realUrl = <span class="hljs-keyword">new</span> URL(url);
<span class="hljs-comment">/*
* http header 参数
*/</span>
<span class="hljs-built_in">String</span> method = <span class="hljs-string">"GET"</span>;
<span class="hljs-built_in">String</span> accept = <span class="hljs-string">"application/json"</span>;
<span class="hljs-built_in">String</span> content_type = <span class="hljs-string">"application/json"</span>;
<span class="hljs-built_in">String</span> path = realUrl.getFile();
<span class="hljs-built_in">String</span> date = toGMTString(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>());
<span class="hljs-comment">// 1.对body做MD5+BASE64加密</span>
<span class="hljs-comment">// String bodyMd5 = MD5Base64(body);</span>
<span class="hljs-built_in">String</span> stringToSign = method + <span class="hljs-string">"\n"</span> + accept + <span class="hljs-string">"\n"</span> + <span class="hljs-string">""</span> + <span class="hljs-string">"\n"</span> + content_type + <span class="hljs-string">"\n"</span> + date + <span class="hljs-string">"\n"</span> + path;
<span class="hljs-comment">// 2.计算 HMAC-SHA1</span>
<span class="hljs-built_in">String</span> signature = HMACSha1(stringToSign, ak_secret);
<span class="hljs-comment">// 3.得到 authorization header</span>
<span class="hljs-built_in">String</span> authHeader = <span class="hljs-string">"Dataplus "</span> + ak_id + <span class="hljs-string">":"</span> + signature;
<span class="hljs-comment">// 打开和URL之间的连接</span>
URLConnection connection = realUrl.openConnection();
<span class="hljs-comment">// 设置通用的请求属性</span>
connection.setRequestProperty(<span class="hljs-string">"accept"</span>, accept);
connection.setRequestProperty(<span class="hljs-string">"content-type"</span>, content_type);
connection.setRequestProperty(<span class="hljs-string">"date"</span>, date);
connection.setRequestProperty(<span class="hljs-string">"Authorization"</span>, authHeader);
connection.setRequestProperty(<span class="hljs-string">"Connection"</span>, <span class="hljs-string">"keep-alive"</span>);
<span class="hljs-comment">// 建立实际的连接</span>
connection.connect();
<span class="hljs-comment">// 定义 BufferedReader输入流来读取URL的响应</span>
statusCode = ((HttpURLConnection)connection).getResponseCode();
<span class="hljs-keyword">if</span>(statusCode != <span class="hljs-number">200</span>) {
<span class="hljs-keyword">in</span> = <span class="hljs-keyword">new</span> BufferedReader(<span class="hljs-keyword">new</span> InputStreamReader(((HttpURLConnection)connection).getErrorStream()));
} <span class="hljs-keyword">else</span> {
<span class="hljs-keyword">in</span> = <span class="hljs-keyword">new</span> BufferedReader(<span class="hljs-keyword">new</span> InputStreamReader(connection.getInputStream()));
}
<span class="hljs-built_in">String</span> line;
<span class="hljs-keyword">while</span> ((line = <span class="hljs-keyword">in</span>.readLine()) != <span class="hljs-literal">null</span>) {
result += line;
}
} <span class="hljs-keyword">catch</span> (Exception e) {
e.printStackTrace();
} <span class="hljs-keyword">finally</span> {
<span class="hljs-keyword">try</span> {
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">in</span> != <span class="hljs-literal">null</span>) {
<span class="hljs-keyword">in</span>.close();
}
} <span class="hljs-keyword">catch</span> (Exception e) {
e.printStackTrace();
}
}
<span class="hljs-keyword">if</span> (statusCode != <span class="hljs-number">200</span>) {
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> IOException(<span class="hljs-string">"\nHttp StatusCode: "</span>+ statusCode + <span class="hljs-string">"\nErrorMessage: "</span> + result);
}
<span class="hljs-keyword">return</span> result;
}
public <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> main(<span class="hljs-built_in">String</span>[] args) throws Exception {
<span class="hljs-comment">// 发送POST请求示例</span>
<span class="hljs-built_in">String</span> ak_id = <span class="hljs-string">"********"</span>; <span class="hljs-comment">//用户ak</span>
<span class="hljs-built_in">String</span> ak_secret = <span class="hljs-string">"********"</span>; <span class="hljs-comment">// 用户ak_secret</span>
<span class="hljs-built_in">String</span> url = <span class="hljs-string">"https://dtplus-cn-shanghai.data.aliyuncs.com/image/scene"</span>;
<span class="hljs-built_in">String</span> body = <span class="hljs-string">"{\"type\": \"0\", \"image_url\":\"https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1544608444&di=54513735567736d0eaeba7a13aa0421c&src=http://img5q.duitang.com/uploads/item/201411/23/20141123205812_Bircn.jpeg\"}"</span>;
System.out.println(<span class="hljs-string">"response body:"</span> + sendPost(url, body, ak_id, ak_secret));
}
}
使用本地图片测试
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Encoder;
import javax.crypto.Mac;
public class ComputerDemo2 {
<span class="hljs-comment">/*
* 计算MD5+BASE64
*/</span>
public <span class="hljs-keyword">static</span> <span class="hljs-built_in">String</span> MD5Base64(<span class="hljs-built_in">String</span> s) {
<span class="hljs-keyword">if</span> (s == <span class="hljs-literal">null</span>)
<span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;
<span class="hljs-built_in">String</span> encodeStr = <span class="hljs-string">""</span>;
byte[] utfBytes = s.getBytes();
MessageDigest mdTemp;
<span class="hljs-keyword">try</span> {
mdTemp = MessageDigest.getInstance(<span class="hljs-string">"MD5"</span>);
mdTemp.update(utfBytes);
byte[] md5Bytes = mdTemp.digest();
BASE64Encoder b64Encoder = <span class="hljs-keyword">new</span> BASE64Encoder();
encodeStr = b64Encoder.encode(md5Bytes);
} <span class="hljs-keyword">catch</span> (Exception e) {
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">"Failed to generate MD5 : "</span> + e.getMessage());
}
<span class="hljs-keyword">return</span> encodeStr;
}
<span class="hljs-comment">/*
* 计算 HMAC-SHA1
*/</span>
public <span class="hljs-keyword">static</span> <span class="hljs-built_in">String</span> HMACSha1(<span class="hljs-built_in">String</span> data, <span class="hljs-built_in">String</span> key) {
<span class="hljs-built_in">String</span> result;
<span class="hljs-keyword">try</span> {
SecretKeySpec signingKey = <span class="hljs-keyword">new</span> SecretKeySpec(key.getBytes(), <span class="hljs-string">"HmacSHA1"</span>);
Mac mac = Mac.getInstance(<span class="hljs-string">"HmacSHA1"</span>);
mac.init(signingKey);
byte[] rawHmac = mac.doFinal(data.getBytes());
result = (<span class="hljs-keyword">new</span> BASE64Encoder()).encode(rawHmac);
} <span class="hljs-keyword">catch</span> (Exception e) {
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">"Failed to generate HMAC : "</span> + e.getMessage());
}
<span class="hljs-keyword">return</span> result;
}
<span class="hljs-comment">/*
* 等同于javaScript中的 new Date().toUTCString();
*/</span>
public <span class="hljs-keyword">static</span> <span class="hljs-built_in">String</span> toGMTString(<span class="hljs-built_in">Date</span> date) {
SimpleDateFormat df = <span class="hljs-keyword">new</span> SimpleDateFormat(<span class="hljs-string">"E, dd MMM yyyy HH:mm:ss z"</span>, Locale.UK);
df.setTimeZone(<span class="hljs-keyword">new</span> java.util.SimpleTimeZone(<span class="hljs-number">0</span>, <span class="hljs-string">"GMT"</span>));
<span class="hljs-keyword">return</span> df.format(date);
}
<span class="hljs-comment">/*
* 发送POST请求
*/</span>
public <span class="hljs-keyword">static</span> <span class="hljs-built_in">String</span> sendPost(<span class="hljs-built_in">String</span> url, <span class="hljs-built_in">String</span> body, <span class="hljs-built_in">String</span> ak_id, <span class="hljs-built_in">String</span> ak_secret) throws Exception {
PrintWriter out = <span class="hljs-literal">null</span>;
BufferedReader <span class="hljs-keyword">in</span> = <span class="hljs-literal">null</span>;
<span class="hljs-built_in">String</span> result = <span class="hljs-string">""</span>;
int statusCode = <span class="hljs-number">200</span>;
<span class="hljs-keyword">try</span> {
URL realUrl = <span class="hljs-keyword">new</span> URL(url);
<span class="hljs-comment">/*
* http header 参数
*/</span>
<span class="hljs-built_in">String</span> method = <span class="hljs-string">"POST"</span>;
<span class="hljs-built_in">String</span> accept = <span class="hljs-string">"application/json"</span>;
<span class="hljs-built_in">String</span> content_type = <span class="hljs-string">"application/json"</span>;
<span class="hljs-built_in">String</span> path = realUrl.getFile();
<span class="hljs-built_in">String</span> date = toGMTString(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>());
<span class="hljs-comment">// 1.对body做MD5+BASE64加密</span>
<span class="hljs-built_in">String</span> bodyMd5 = MD5Base64(body);
<span class="hljs-built_in">String</span> stringToSign = method + <span class="hljs-string">"\n"</span> + accept + <span class="hljs-string">"\n"</span> + bodyMd5 + <span class="hljs-string">"\n"</span> + content_type + <span class="hljs-string">"\n"</span> + date + <span class="hljs-string">"\n"</span>
+ path;
<span class="hljs-comment">// 2.计算 HMAC-SHA1</span>
<span class="hljs-built_in">String</span> signature = HMACSha1(stringToSign, ak_secret);
<span class="hljs-comment">// 3.得到 authorization header</span>
<span class="hljs-built_in">String</span> authHeader = <span class="hljs-string">"Dataplus "</span> + ak_id + <span class="hljs-string">":"</span> + signature;
<span class="hljs-comment">// 打开和URL之间的连接</span>
URLConnection conn = realUrl.openConnection();
<span class="hljs-comment">// 设置通用的请求属性</span>
conn.setRequestProperty(<span class="hljs-string">"accept"</span>, accept);
conn.setRequestProperty(<span class="hljs-string">"content-type"</span>, content_type);
conn.setRequestProperty(<span class="hljs-string">"date"</span>, date);
conn.setRequestProperty(<span class="hljs-string">"Authorization"</span>, authHeader);
<span class="hljs-comment">// 发送POST请求必须设置如下两行</span>
conn.setDoOutput(<span class="hljs-literal">true</span>);
conn.setDoInput(<span class="hljs-literal">true</span>);
<span class="hljs-comment">// 获取URLConnection对象对应的输出流</span>
out = <span class="hljs-keyword">new</span> PrintWriter(conn.getOutputStream());
<span class="hljs-comment">// 发送请求参数</span>
out.print(body);
<span class="hljs-comment">// flush输出流的缓冲</span>
out.flush();
<span class="hljs-comment">// 定义BufferedReader输入流来读取URL的响应</span>
statusCode = ((HttpURLConnection)conn).getResponseCode();
<span class="hljs-keyword">if</span>(statusCode != <span class="hljs-number">200</span>) {
<span class="hljs-keyword">in</span> = <span class="hljs-keyword">new</span> BufferedReader(<span class="hljs-keyword">new</span> InputStreamReader(((HttpURLConnection)conn).getErrorStream()));
} <span class="hljs-keyword">else</span> {
<span class="hljs-keyword">in</span> = <span class="hljs-keyword">new</span> BufferedReader(<span class="hljs-keyword">new</span> InputStreamReader(conn.getInputStream()));
}
<span class="hljs-built_in">String</span> line;
<span class="hljs-keyword">while</span> ((line = <span class="hljs-keyword">in</span>.readLine()) != <span class="hljs-literal">null</span>) {
result += line;
}
} <span class="hljs-keyword">catch</span> (Exception e) {
e.printStackTrace();
} <span class="hljs-keyword">finally</span> {
<span class="hljs-keyword">try</span> {
<span class="hljs-keyword">if</span> (out != <span class="hljs-literal">null</span>) {
out.close();
}
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">in</span> != <span class="hljs-literal">null</span>) {
<span class="hljs-keyword">in</span>.close();
}
} <span class="hljs-keyword">catch</span> (IOException ex) {
ex.printStackTrace();
}
}
<span class="hljs-keyword">if</span> (statusCode != <span class="hljs-number">200</span>) {
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> IOException(<span class="hljs-string">"\nHttp StatusCode: "</span>+ statusCode + <span class="hljs-string">"\nErrorMessage: "</span> + result);
}
<span class="hljs-keyword">return</span> result;
}
<span class="hljs-comment">/*
* GET请求
*/</span>
public <span class="hljs-keyword">static</span> <span class="hljs-built_in">String</span> sendGet(<span class="hljs-built_in">String</span> url, <span class="hljs-built_in">String</span> ak_id, <span class="hljs-built_in">String</span> ak_secret) throws Exception {
<span class="hljs-built_in">String</span> result = <span class="hljs-string">""</span>;
BufferedReader <span class="hljs-keyword">in</span> = <span class="hljs-literal">null</span>;
int statusCode = <span class="hljs-number">200</span>;
<span class="hljs-keyword">try</span> {
URL realUrl = <span class="hljs-keyword">new</span> URL(url);
<span class="hljs-comment">/*
* http header 参数
*/</span>
<span class="hljs-built_in">String</span> method = <span class="hljs-string">"GET"</span>;
<span class="hljs-built_in">String</span> accept = <span class="hljs-string">"application/json"</span>;
<span class="hljs-built_in">String</span> content_type = <span class="hljs-string">"application/json"</span>;
<span class="hljs-built_in">String</span> path = realUrl.getFile();
<span class="hljs-built_in">String</span> date = toGMTString(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>());
<span class="hljs-comment">// 1.对body做MD5+BASE64加密</span>
<span class="hljs-comment">// String bodyMd5 = MD5Base64(body);</span>
<span class="hljs-built_in">String</span> stringToSign = method + <span class="hljs-string">"\n"</span> + accept + <span class="hljs-string">"\n"</span> + <span class="hljs-string">""</span> + <span class="hljs-string">"\n"</span> + content_type + <span class="hljs-string">"\n"</span> + date + <span class="hljs-string">"\n"</span> + path;
<span class="hljs-comment">// 2.计算 HMAC-SHA1</span>
<span class="hljs-built_in">String</span> signature = HMACSha1(stringToSign, ak_secret);
<span class="hljs-comment">// 3.得到 authorization header</span>
<span class="hljs-built_in">String</span> authHeader = <span class="hljs-string">"Dataplus "</span> + ak_id + <span class="hljs-string">":"</span> + signature;
<span class="hljs-comment">// 打开和URL之间的连接</span>
URLConnection connection = realUrl.openConnection();
<span class="hljs-comment">// 设置通用的请求属性</span>
connection.setRequestProperty(<span class="hljs-string">"accept"</span>, accept);
connection.setRequestProperty(<span class="hljs-string">"content-type"</span>, content_type);
connection.setRequestProperty(<span class="hljs-string">"date"</span>, date);
connection.setRequestProperty(<span class="hljs-string">"Authorization"</span>, authHeader);
connection.setRequestProperty(<span class="hljs-string">"Connection"</span>, <span class="hljs-string">"keep-alive"</span>);
<span class="hljs-comment">// 建立实际的连接</span>
connection.connect();
<span class="hljs-comment">// 定义 BufferedReader输入流来读取URL的响应</span>
statusCode = ((HttpURLConnection)connection).getResponseCode();
<span class="hljs-keyword">if</span>(statusCode != <span class="hljs-number">200</span>) {
<span class="hljs-keyword">in</span> = <span class="hljs-keyword">new</span> BufferedReader(<span class="hljs-keyword">new</span> InputStreamReader(((HttpURLConnection)connection).getErrorStream()));
} <span class="hljs-keyword">else</span> {
<span class="hljs-keyword">in</span> = <span class="hljs-keyword">new</span> BufferedReader(<span class="hljs-keyword">new</span> InputStreamReader(connection.getInputStream()));
}
<span class="hljs-built_in">String</span> line;
<span class="hljs-keyword">while</span> ((line = <span class="hljs-keyword">in</span>.readLine()) != <span class="hljs-literal">null</span>) {
result += line;
}
} <span class="hljs-keyword">catch</span> (Exception e) {
e.printStackTrace();
} <span class="hljs-keyword">finally</span> {
<span class="hljs-keyword">try</span> {
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">in</span> != <span class="hljs-literal">null</span>) {
<span class="hljs-keyword">in</span>.close();
}
} <span class="hljs-keyword">catch</span> (Exception e) {
e.printStackTrace();
}
}
<span class="hljs-keyword">if</span> (statusCode != <span class="hljs-number">200</span>) {
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> IOException(<span class="hljs-string">"\nHttp StatusCode: "</span>+ statusCode + <span class="hljs-string">"\nErrorMessage: "</span> + result);
}
<span class="hljs-keyword">return</span> result;
}
public <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> main(<span class="hljs-built_in">String</span>[] args) throws Exception {
<span class="hljs-comment">// 发送POST请求示例</span>
<span class="hljs-built_in">String</span> ak_id = <span class="hljs-string">"********"</span>; <span class="hljs-comment">//用户ak</span>
<span class="hljs-built_in">String</span> ak_secret = <span class="hljs-string">"********"</span>; <span class="hljs-comment">// 用户ak_secret</span>
<span class="hljs-built_in">String</span> url = <span class="hljs-string">"https://dtplus-cn-shanghai.data.aliyuncs.com/image/scene"</span>;
<span class="hljs-comment">//上传本地图片</span>
<span class="hljs-comment">// Request body</span>
<span class="hljs-built_in">String</span> pic_path = <span class="hljs-string">"C:\\Users\\taro.yt\\Desktop\\timg.jpg"</span>;
File picBase64 = <span class="hljs-keyword">new</span> File(pic_path);
<span class="hljs-built_in">String</span> pic = encodeImageToBase64(picBase64);
<span class="hljs-comment">//提出base64编码的换行符问题</span>
<span class="hljs-built_in">String</span> data = pic.replaceAll(<span class="hljs-string">"[\\s*\t\n\r]"</span>, <span class="hljs-string">""</span>);
data = <span class="hljs-string">"'"</span> + data + <span class="hljs-string">"'"</span>;
<span class="hljs-built_in">String</span> body = <span class="hljs-string">"{\"type\": \"1\", \"content\":"</span>+data+<span class="hljs-string">"}"</span>;
System.out.println(<span class="hljs-string">"response body:"</span> + sendPost(url, body, ak_id, ak_secret));
}
<span class="hljs-comment">/**
* 将本地图片编码为base64
*
* @param file
* @return
* @throws Exception
*/</span>
public <span class="hljs-keyword">static</span> <span class="hljs-built_in">String</span> encodeImageToBase64(File file) throws Exception {
<span class="hljs-comment">//将图片文件转化为字节数组字符串,并对其进行Base64编码处理</span>
// loggerger.info("图片的路径为:" + file.getAbsolutePath());
InputStream <span class="hljs-keyword">in</span> = <span class="hljs-literal">null</span>;
byte[] data = <span class="hljs-literal">null</span>;
<span class="hljs-comment">//读取图片字节数组</span>
<span class="hljs-keyword">try</span> {
<span class="hljs-keyword">in</span> = <span class="hljs-keyword">new</span> FileInputStream(file);
data = <span class="hljs-keyword">new</span> byte[<span class="hljs-keyword">in</span>.available()];
<span class="hljs-keyword">in</span>.read(data);
<span class="hljs-keyword">in</span>.close();
} <span class="hljs-keyword">catch</span> (IOException e) {
e.printStackTrace();
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> Exception(<span class="hljs-string">"图片上传失败,请联系客服!"</span>);
}
<span class="hljs-comment">//对字节数组Base64编码</span>
BASE64Encoder encoder = <span class="hljs-keyword">new</span> BASE64Encoder();
<span class="hljs-built_in">String</span> base64 = encoder.encode(data);
<span class="hljs-keyword">return</span> base64;<span class="hljs-comment">//返回Base64编码过的字节数组字符串</span>
}
}
测试结果
response body:{"tags":[{"value":"人物","confidence":97.0},{"value":"演出","confidence":12.0},{"value":"其他","confidence":12.0},{"value":"室内","confidence":11.0}],"errno":0,"request_id":"b31aebed-305a-4377-a843-ac4e9d3d30c1"}