华为云obs文件存储返回路径:
https://xxx/桶名/文件名?AccessKeyId=xxx&Policy=xxxSignature=xxx
三个参数不多介绍,具体可参考官方文档
废话不多说,直接上代码
```java
//判断是否包含https请求
private static final String HTTPS = "https";
//超时时间
private static final int TIMEOUT = 180000;
public static byte[] getUrlImg(String remoteFilePath) throws Exception {
//remoteFilePath返回的链接
if (!remoteFilePath.contains(HTTPS)) {
URL urlfile;
HttpURLConnection httpUrl;
InputStream inputStream;
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
urlfile = new URL(remoteFilePath);
httpUrl = (HttpURLConnection) urlfile.openConnection();
httpUrl.connect();
httpUrl.setConnectTimeout(TIMEOUT);
httpUrl.setReadTimeout(TIMEOUT);
inputStream = httpUrl.getInputStream();
int len = 2048;
byte[] b = new byte[len];
int num = inputStream.read(b);
while (num != -1) {
baos.write(b, 0, num);
num = inputStream.read(b);
}
baos.flush();
httpUrl.disconnect();
return baos.toByteArray();
}
}
}
上述代码最后返回的是一个byte数组,可根据自己的需求修改返回值,例如想要返回base64:
```java
Base64Utils.encodeToString(返回值)即可得到文件的base64