Spring Boot接收Base64或者图片网址,获取图片流,保存到文件

传入图片网址,获取图片流,保存到文件

  @RequestMapping(value = "/tempTest/getPicture", method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public Result getPicture(String urlParam,String base64Str,HttpServletRequest request,HttpServletResponse response){
        byte[] pictureBytes = null;
        try {
         z  //base64字符串转化成图片流
            if (StringUtils.isNotBlank(base64Str)){
               pictureBytes = Base64Decoder.decode(base64Str);
                //for (int i = 0; i < pictureBytes.length; i++) {
                //    //调整异常数据据  不调整也没啥影响,不知道为啥要这样
                //    if (pictureBytes[i] < 0) {
                //        pictureBytes[i] += 256;
                //    }
                //}
         
            }
            if (StringUtils.isBlank(urlParam)) {
                urlParam = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2Ftp01%2F1ZZQ20QJS6-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1650179194&t=1dda4ab6861789d5a68ee9e78783ae66";
                URL url = new URL(urlParam);
                // 打开连接
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                // 设置一些参数
                conn.setDoOutput(true);
                conn.setDoInput(true);
                conn.setUseCaches(false);
                conn.connect();
                // 得到输入流
                InputStream inputStream = conn.getInputStream();
                byte[] buf = new byte[1024];
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                int len = 0;
                while ((len = inputStream.read(buf)) != -1) {
                    outputStream.write(buf, 0, len);
                }
                outputStream.close();
                inputStream.close();
                byte[] bytes = outputStream.toByteArray();
                //return bytes;
                pictureBytes = bytes;
            }
            //获取jar包路径
            String jar_parent = new File(ResourceUtils.getURL("classpath:").getPath()).getParentFile().getParentFile().getParent();
            log.info("当前项目根目录:{}", jar_parent);
            jar_parent = jar_parent.replace("\\", File.separator);
            log.info("jar_parent:{}", jar_parent);
            //linux下获取到路径是  file: 开头,要把file:去掉
            jar_parent = jar_parent.substring(5);
            log.info("处理后jar_parent:{}", jar_parent);

            //按照日期创建文件夹
            String yyyyMMdd = DateUtils.dateToString(new Date(), "yyyyMMdd");
            File file = new File(jar_parent, "staticPicture" + File.separator + yyyyMMdd);
            if (!file.exists()) {
                boolean mkdirs = file.mkdirs();
                log.info("文件创建结果:{}", mkdirs);
            }
            String pictureName = UUID.randomUUID().toString().replaceAll("-", "") + ".png";
            String pictureFilePath = jar_parent + File.separator + "staticPicture" + File.separator + yyyyMMdd + File.separator + pictureName;
            File pictureFile = new File(pictureFilePath);
            if (!pictureFile.exists()) {
                boolean newFile = pictureFile.createNewFile();
            }
            FileOutputStream out = new FileOutputStream(pictureFile);
            out.write(pictureBytes);
            out.flush();
            out.close();
            return ResultUtil.success(pictureName);

        } catch (Exception e) {
            log.error("保存图片过程异常");
            e.printStackTrace();
            return ResultUtil.error(ResultCode.API_ERROR);
        }
    }

其他相关资源:

springboot上传文件(前端传来的是base64字符串形式)

Java实现图片转换base64的两种方法,你值得拥有!

Spring Boot通过Base64上传图片

base解码问题,为什么要b[i]+=256?

springboot实战代码之如何优雅地返回图片

springboot 返回图片流验证码 并进行验证是否正确 vue获取到图片进行展示 登录自动验证。

https://blog.csdn.net/sinat_39327967/article/details/117370505

spring-boot返回文件流

Springboot中如何将本地文件以流的形式返回给前端下载

SpringBoot如何优雅的使用@ResponseBody返回图片

你可能感兴趣的:(SpringBoot,spring,boot,java)