java内网_java下载内网图片给前端

@PostMapping("/view")

public ApiResponse view(@RequestBody JSONObject json, HttpServletRequest request,HttpServletResponse response) throws Exception {

String userId = getUserId(request);

if (StringUtils.isEmpty(userId)) {

return ResponseCode.response(ResponseCode.token_error);

}

//获取到上传的文件数据

Object imgUrlObj = json.get("imgUrl");

if (imgUrlObj == null) {

return new ApiResponse(ResponseCode.parameter_error.getCode(), "imgUrl不能为空");

}

String url = imgUrlObj.toString();

BufferedImage img = FileUtil.getBufferedImage(url);

if (img == null) {

throw new GAPOIException(GapoiResponseCode.URL_IS_NOT_IMG);

}

String format = url.substring(url.lastIndexOf(".") + 1);

ImageIO.write(img, format, response.getOutputStream());

return null;

}

/**

* httpurl图片路径 返回BufferedImage

* @param url

* @return

* @throws Exception

*/

public static BufferedImage getBufferedImage(String url) throws Exception{

URL readUrl = new URL(url);

URLConnection urlConnection = readUrl.openConnection();

urlConnection.setConnectTimeout(1000);

urlConnection.setReadTimeout(5000);

urlConnection.connect();

InputStream inputStream = urlConnection.getInputStream();

return ImageIO.read(inputStream);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值