zoukankan      html  css  js  c++  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);
    }
  • 相关阅读:
    P3368 【模板】树状数组 2
    P3374 【模板】树状数组 1
    BZOJ 2654
    BZOJ 1016
    BZOJ 4870
    BZOJ 4868
    BZOJ 1503
    P3379 【模板】最近公共祖先(LCA)
    雅礼2017国庆1-1
    使用 CefSharp 网页显示问题
  • 原文地址:https://www.cnblogs.com/zfzf1/p/11882769.html
Copyright © 2011-2022 走看看