byte[] bytes = OSSUtil.downLoadFile(creditImportVO.getFile());
/**
* 获取网络文件(不存在为NULL)
*
* @param filePath 文件URL
* @return 文件流
* @throws Exception
*/
public static byte[] downLoadFile(String filePath) throws Exception {
byte[] bytes;
URL url = new URL(filePath);
URLConnection urlConnection = url.openConnection();
urlConnection.connect();
HttpURLConnection httpConnection = (HttpURLConnection) urlConnection;
int httpResult = httpConnection.getResponseCode();
if (HttpURLConnection.HTTP_NOT_FOUND == httpResult) {
log.error("OSS服务器文件不存在!");
return null;
}
if (httpResult != HttpURLConnection.HTTP_OK) {
log.error("从OSS服务器读取文件失败!");
throw new Exception("从OSS服务器读取文件失败!");
} else {
urlConnection.getInputStream();
InputStream inputStream = urlConnection.getInputStream();
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
int ch;
while ((ch = inputStream.read()) != -1) {
swapStream.write(ch);
}
bytes = swapStream.toByteArray();
}
return bytes;
}