1、发送图片

public void getPhotoFile(String photoPath, HttpServletResponse response) throws IOException {
        if (org.apache.commons.lang3.StringUtils.isNotBlank(photoPath)) {
            BufferedImage bufferedImage = ImageIO.read(new FileInputStream(new File(photoPath)));
            // 把图片文件标记为流,方便前端处理
            response.setContentType("application/octet-stream");
            ServletOutputStream out = response.getOutputStream();
            ImageIO.write(bufferedImage, "png", out);
            out.flush();
        }
    }

2、接收文件

    public void saveFile(String path, MultipartFile file) {
        if (file != null && !file.isEmpty()) {
            //保存文件到对应位置
            File dir = new File(path);
            if (!dir.getParentFile().exists()) {
                dir.getParentFile().mkdirs();
            }
            try {
                file.transferTo(dir);
            } catch (IOException e) {
                //抛出异常
            }
        }
    }

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐