上传图片

  • ImageUploadService.java
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Service
public class ImageUploadService {

    /**
     * 功能:转化文件名字   abc.jpg---->>abc_5627.jpg
     *
     * @param fileName 原图片名称
     * @return 转化后的名称
     */
    public String convert(String fileName) {
        String time = (System.currentTimeMillis() + "").substring(9);
        int index = fileName.lastIndexOf(".");
        String suffix = fileName.substring(index);
        String prefix = fileName.substring(0, index);
        fileName = prefix + "_" + time + suffix;
        return fileName;
    }

    /**
     * 功能:上传图片
     *
     * @param file   文件
     * @param reload 服务器上上传文件的路径,   例如:http://106.15.192.117:8080/picture/
     * @param load   服务器上上传文件的物理路径,例如:F:\others\Desktop\666\
     * @return 上传文件的 URL相对地址
     */
    public String uploadImage(MultipartFile file, String reload, String load) {

        String fileName = convert(file.getOriginalFilename());
        String filePath = load + fileName;

        try {
            File targetFile = new File(filePath);
            FileUtils.writeByteArrayToFile(targetFile, file.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return reload + "/" + fileName;
    }
}
  • UploadController.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class UploadController {

    @Autowired
    private ImageUploadService imageUploadService;

    @Autowired
    private ImageWatermarkService watermarkService;

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public String watermarkTest(@RequestParam("file") MultipartFile image) {

        ImageInfo imgInfo = new ImageInfo();

        final String reload = "http://106.15.192.117:8080/pic/";//返回的路径
        final String load = "/usr/share/tomcat8/webapps/pic/";//实际路径

        String imageURL = imageUploadService.uploadImage(image, reload, load);

        return imageURL;
    }
}



添加水印

  • 实体类ImageInfo.java
public class ImageInfo {
    private String imageUrl;//未加水印图片路径
    private String logoImageUrl;//加水印图片路径

    public String getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }

    public String getLogoImageUrl() {
        return logoImageUrl;
    }

    public void setLogoImageUrl(String logoImageUrl) {
        this.logoImageUrl = logoImageUrl;
    }
}
  • service类
    • ImageUploadService.java
      见上方

    • ImageWatermarkService.java

    import com.sun.image.codec.jpeg.JPEGCodec;
    import com.sun.image.codec.jpeg.JPEGImageEncoder;
    import org.springframework.stereotype.Service;
    
    import javax.imageio.ImageIO;
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    
    @Service
    public class ImageWatermarkService {
    
        /**
         * imgFile 图像文件
         * reload 服务器上上传文件的相对路径
         * load 服务器上上传文件的物理路径
         * final logoPath--->水印图片的路径
         */
        public String watermarkAdd(File imgFile, String reload, String load) {
            final String logoPath = "F:\\others\\Desktop\\666\\aa.jpg";  // 水印图片地址
    
            String imgWithWatermarkFileName = "watermark_" + imgFile.getName();
            OutputStream os = null;
    
            try {
                Image image = ImageIO.read(imgFile);
    
                int width = image.getWidth(null);
                int height = image.getHeight(null);
    
                BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  // ①
                Graphics2D g = bufferedImage.createGraphics();  // ②
                g.drawImage(image, 0, 0, width, height, null);  // ③
    
                File logo = new File(logoPath);        // 读取水印图片
                Image imageLogo = ImageIO.read(logo);
    
                int markWidth = imageLogo.getWidth(null);    // 水印图片的宽度和高度
                int markHeight = imageLogo.getHeight(null);
    
                g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.2f));  // 设置水印透明度
                g.rotate(Math.toRadians(-10), bufferedImage.getWidth() / 2, bufferedImage.getHeight() / 2);  // 设置水印图片的旋转度
    
                int x = 20;
                int y = 20;
    
                int xInterval = 10;
                int yInterval = 10;
    
                double count = 1.5;
                while (x < width * count) {  // 循环添加多个水印logo
                    y = -height / 2;
                    while (y < height * count) {
                        g.drawImage(imageLogo, x, y, null);  // ④
                        y += markHeight + yInterval;
                    }
                    x += markWidth + xInterval;
                }
    
                g.dispose();
    
                os = new FileOutputStream(load + "/" + imgWithWatermarkFileName);
                JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os); // ⑤
                en.encode(bufferedImage); // ⑥
    
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (os != null) {
                    try {
                        os.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return reload + "/" + imgWithWatermarkFileName;
        }
    }
    
    
  • WatermarkController.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@RestController
public class WatermarkController {

    @Autowired
    private ImageUploadService imageUploadService;

    @Autowired
    private ImageWatermarkService watermarkService;

    @RequestMapping(value = "/watermarktest", method = RequestMethod.POST)
    public ImageInfo watermarkTest(@RequestParam("file") MultipartFile image) {

        ImageInfo imgInfo = new ImageInfo();

        final String reload = "http://106.15.192.117:8080/pic/";//返回的路径
        final String load = "F:\\others\\Desktop\\666\\";//实际路径

        //未加水印的图片的路径
        String imageURL = imageUploadService.uploadImage(image, reload, load);

        //加水印的路径
        File imageFile = new File(load + imageURL.substring(imageURL.lastIndexOf("/")));
        String watermarkAddImageURL = watermarkService.watermarkAdd(imageFile, reload, load);

        imgInfo.setImageUrl(imageURL);
        imgInfo.setLogoImageUrl(watermarkAddImageURL);
        return imgInfo;
    }
}


转载于https://www.codesheep.cn/


hhhhh