java springboot图片上传和访问

上传

@RequestMapping("/uploadImg")
    public Result uploadImg(HttpServletRequest request, MultipartFile file){
        log.info("/uploadImg->上传图片->开始" );
        JSONObject jsonObject = new JSONObject();
        String mid = request.getParameter("mid");
        //String rootPath = System.getProperty("catalina.home");
        //rootPath = rootPath + "/" + image_path;
        String rootPath = image_path;
        File f = new File(rootPath);
        if(!f.exists()){
            f.mkdir();
        }
        if (StringUtils.isBlank(mid)){
            return Result.error("mid is error");
        }
        String uuidStr = UUID.randomUUID().toString().replace("-","");
        String fileName = uuidStr + ".png";
        String filePath = rootPath + "/" + fileName;//本地绝对路径
        String filePathTmp = rootPath + "/" + uuidStr + "_tmp.png";
        log.info("[图片接口]上传的图片本地绝对路径为->" + filePath);
        f = new File(filePath);
        try {
            file.transferTo(f);
        } catch (IOException e) {
            e.printStackTrace();
            throw new AbpException("[图片接口]写文件到磁盘失败");
        }
        if(UtilImg.createThumbnail(filePath,filePathTmp, Float.parseFloat(image_width),Float.parseFloat(image_height))){
            UtilImg.delelteAndRenameFile(filePath,filePathTmp,uuidStr);
        }
        jsonObject.put("path",fileName);
        log.info("/uploadImg->上传图片->结束->" + jsonObject.toJSONString() );
        return Result.ok(jsonObject);
    }

访问

@RequestMapping("/getImg/{filename}")
    public void getImg(HttpServletResponse response, @PathVariable("filename") String filename){
        log.info("/getImg->访问图片->开始" );
        if(StringUtils.isBlank(filename)){
            throw new AbpException("[图片接口]参数有误");
        }
        //String rootPath = System.getProperty("catalina.home");
        //String filePath = rootPath + "/" + image_path + "/" + filename;
        String rootPath = image_path;
        String filePath = rootPath + "/" + filename;
        File imageFile = new File(filePath);
        if (imageFile.exists()){
            FileInputStream fis = null;
            OutputStream os = null;
            try {
                fis = new FileInputStream(imageFile);
                os = response.getOutputStream();
                int count = 0;
                byte[] buffer = new byte[1024 * 8];
                while((count = fis.read(buffer)) != -1){
                    os.write(buffer,0,count);
                    os.flush();
                }
                log.info("[图片接口]输出完成");
            } catch (Exception e) {
                e.printStackTrace();
                throw new AbpException("[图片接口]打开图片失败,可能图片ID错误");
            }finally {
                if(fis != null){
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                        throw new AbpException("[图片接口]关闭输入流失败");
                    }
                }
                if(os != null){
                    try {
                        os.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                        throw new AbpException("[图片接口]关闭输出流失败");
                    }
                }

            }

        }else{
            throw new AbpException("[图片接口]图片ID错误");
        }
    }

上传的接口文档

访问的接口文档

posted @ 2022-03-14 09:36  DaenMax  阅读(1342)  评论(0编辑  收藏  举报