/**
* 从腾讯云下载图片
*
* @param map mediaUrl 图片路径 //https://efuav-1302980652.cos.ap-shanghai.myqcloud.com/无人机/07JDE6E0020240/thumbnail/20220211/dji20220211154928.dng
* @return 成功失败
*/
@PostMapping(value = "/downLoadMedia")
public Result uploadMedia(@RequestBody Map<String, Object> map, HttpServletRequest request, HttpServletResponse response) throws IOException {
try {
String mediaUrl = map.getOrDefault("mediaUrl", "").toString();
URL urlfile = null;
HttpURLConnection httpUrl = null;
BufferedInputStream bis = null;
OutputStream outputStream = null;
try {
urlfile = new URL(mediaUrl);
httpUrl = (HttpURLConnection) urlfile.openConnection();
httpUrl.connect();
bis = new BufferedInputStream(httpUrl.getInputStream());
outputStream = new BufferedOutputStream(response.getOutputStream());
//创建存放文件内容的数组
byte[] buff = new byte[1024];
//所读取的内容使用n来接收
int n;
LogUtil.logInfo(String.valueOf(bis.available()));
response.setContentLength((int) bis.available());
//当没有读取完时,继续读取,循环
while ((n = bis.read(buff)) != -1) {
//将字节数组的数据全部写入到输出流中
outputStream.write(buff, 0, n);
}
//强制将缓存区的数据进行输出
outputStream.flush();
//关流
outputStream.close();
bis.close();
} catch (Exception e) {
e.printStackTrace();
LogUtil.logError(e.getMessage());
} finally {
try {
bis.close();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return ResultUtil.success("图片下载成功!");
} catch (Exception e) {
LogUtil.logError("下载图片出错:" + e.toString());
return ResultUtil.error("下载图片出错,请联系管理员!");
}
}
java从远端url读取图片并且推送给前端
猜你喜欢
转载自blog.csdn.net/m0_61367109/article/details/122998662
今日推荐
周排行