主要用于sftp,ftp使用,所以分类就放在 ftp算了
/**
* 根据阿里云图片路径生成byte字节数组
*
* @param img 阿里云图片路径
* @return 返回图片的字节数组
*/
public byte[] imgPathBytes(String img) {
// 设置参数
InputStream in = null;
ByteArrayOutputStream byteArrOps = null;
int length;
byte[] buffer = new byte[1024 * 5];
byte[] data = null;
try {
URL url = new URL(img);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setReadTimeout(1000 * 60);
if (urlConnection.getResponseCode() == 200) {
in = urlConnection.getInputStream();
byteArrOps = new ByteArrayOutputStream();
while ((length = in.read(buffer)) != -1) {
byteArrOps.write(buffer, 0, length);
}
byteArrOps.flush();
data = byteArrOps.toByteArray();
// 下载大小: data.length
}
} catch (Exception e) {
log.error("图片上传出现异常: {}, url为: {}", e, img);
return null;
} finally {
try {
if (byteArrOps != null) {
byteArrOps.close();
}
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
log.error("关闭异常", e);
}
}
return data;
}