Spring Boot 所有文件下载,图片下载加水印

一、图片的预览和文件的下载,用FreeMaork不用写代码就可以轻松搞定,template-loader-path是文件的位置,可以添加多个文件访问地址,用逗号隔开。

spring:
   resources:
      static-locations: file:/Users/jiangxiaoyi/Desktop/files
   freemarker:
  #    设定模板的加载路径,多个以逗号分隔,默认: ["classpath:/templates/"]
      template-loader-path: file:/Users/jiangxiaoyi/Desktop/files
  #    设定所有request的属性在merge到模板的时候,是否要都添加到model中.
      request-context-attribute: request
  #    设定Template的编码.
      charset: UTF-8
  #    是否检查templates路径是否存在.
      check-template-location: true
  #    是否允许mvc使用freemarker.
      enabled: true
  #    设定模板的后缀.
      suffix: .html
      settings:
  #    更新即时生效
        template_update_delay: 0

然后通过url直接请求就可以实现文件下载啦,http://loacalhoost/项目目名称/文件名字

 

 

二、对上传的文件大小进行限制,在application.yml的Spring下加入

spring:
 servlet:
     multipart:
        max-file-size: 50MB
        max-request-size: 500MB

如果启动报错记得MB都大写。

 

三、图片下载加水印

先说一下图片下载时加水印的步骤:

1.先获取到需要下载的文件

2.对原文件加入水印

3加完水印的二进制流可以重新也可以重新生成一个新的文件,如果为了保留原文件最好是生成一个新的图片。

4.对新生的文件下载

public void downloadWithWaterMark(HttpServletRequest request, RequestVo requestVo, HttpServletResponse response) {
    String fileName = requestVo.getFileName();
    String suffix = Tools.getFileSuffix(fileName);
    String waterFilePath = null;
    if (Arrays.asList(Consts.constant.PICTURE_SUFFIX).contains(suffix)) {
       //图片加水印
        waterFilePath = waterMarkForPic(requestVo);
       // 下载
       downloadFile(waterFilePath, response);
    }     
}
如果对上传的图片格式有限制加上,PICTURE_SUFFIX = {".apk", ".jpg", ".jpeg", ".png", ".JPG", ".jpeg", ".PNG", ".gif", ".GIF"};

 

(一)、图片加水印

   //图片加水印
    public String waterMarkForPic(RequestVo requestVo) {
        String fileName = requestVo.getFileName();
        //水印内容
        String waterMarkContent = "xxxxxxxx";
        //字体颜色,不知道数据没有关系可以点左侧的颜色自行选择的
        Color color = new Color(221, 75, 57);
        //字体和字号的设置
        Font font = new Font("Times New Roman", Font.PLAIN, 120);
        //对字体的透明度设置
        float alpht = 0.5f;
        //加上水印图片的路径,如果路径一致,名字要不同否则是对原文件进行操作
        String tarImgPath = Consts.constant.UPLOAD_LOCAL_FILE_ADDRESS_PREFIX + "/" + request.getproject() + "/" + "water-"+fileName;
        try {
            // 读取原图片信息
            String filePath = Consts.constant.UPLOAD_LOCAL_FILE_ADDRESS_PREFIX + "/" + request.getproject()) + "/" + fileName;
            //得到文件
            File srcImgFile = new File(filePath);
            //文件转化为图片
            Image srcImg = ImageIO.read(srcImgFile);
            //获取图片的宽
            int srcImgWidth = srcImg.getWidth(null);
            //获取图片的高
            int srcImgHeight = srcImg.getHeight(null);
            // 加水印
            BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics2D = bufImg.createGraphics();
            graphics2D.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
            //颜色
            graphics2D.setColor(color); 
            //字体
            graphics2D.setFont(font);
            //透明
            graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,alpht));
            //旋转
            graphics2D.rotate(Math.toRadians(45),(double)bufImg.getWidth()/2,(double)bufImg.getHeight()/2);
            //设置水印的坐标,这可以根据打印的内容大小和位置决定
            int x = srcImgWidth/50;
            int y = srcImgHeight/2;
            //画出水印
            graphics2D.drawString(waterMarkContent, x, y);  
            graphics2D.dispose();
            // 输出图片
            FileOutputStream outImgStream = new FileOutputStream(tarImgPath);
            ImageIO.write(bufImg, "jpg", outImgStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //返回加了谁演的图片路径
        return tarImgPath;
    }

(二)、文件下载(所有文件都适用)

public void downloadFile(String path, HttpServletResponse response) {

    String filePath = path.substring(0, path.lastIndexOf("/"));
    String fileName = path.substring(path.lastIndexOf("/"),path.length());
    File file = new File(filePath);
    if (file.exists()) {
        response.setContentType("application/force-download");
        response.addHeader("Content-Disposition","attachment;" + "fileName=" + fileName);
        byte[] buffer = new byte[1024];
        FileInputStream fileInputStream = null;
        BufferedInputStream bufferedInputStream = null;
        try {
            fileInputStream = new FileInputStream(new File(path));
            bufferedInputStream = new BufferedInputStream(fileInputStream);
            OutputStream outputStream = response.getOutputStream();
            int i = bufferedInputStream.read(buffer);
            while (i != Consts.constant.NAGATIVE_ONE) {
                outputStream.write(buffer, 0, i);
                i = bufferedInputStream.read(buffer);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedInputStream != null) {
                try {
                    bufferedInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 

 

你可能感兴趣的:(Spring Boot 所有文件下载,图片下载加水印)