java更改圖片格式,,更改圖片大小,並實現多線程上傳圖片。


package com.tongyou.util;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 * 文件上傳工具
 * @version 1.0.0
 * @date 2018/4/2 14:00
 */
public class UploadUtil implements Runnable {

    private static final Logger LOG = LoggerFactory.getLogger(UploadUtil.class);

    private MultipartFile multipartFile;

    /**
     * 根路徑
     */
    private String rootPath;

    /**
     * 文件夾全路徑
     */
    private String folderPath;

    /**
     * 文件全路徑
     */
    private String filePath;

    /**
     * 文件訪問地址
     */
    private String fileUrl;

    public UploadUtil(MultipartFile multipartFile, String folderName) {
        this.multipartFile = multipartFile;
        this.rootPath = "V:/XPH/image";
        String realFilename=multipartFile.getOriginalFilename();
        String fileExtension = realFilename.substring(realFilename.lastIndexOf("."));
        String fileName= UUIDUtil.randomUUID() + fileExtension;
        this.folderPath = this.rootPath + folderName;
        this.filePath = folderPath + "/" + fileName;
        String fileUrl = "http://192.168.0.148:80" + folderName + "/" + fileName;
        this.fileUrl = fileUrl.substring(0, fileUrl.lastIndexOf("."));
    }

    private void upload() throws Exception {
        File dirPath = new File(folderPath);
        if (!dirPath.exists()) {
            dirPath.mkdirs();
        }
        File uploadFile = new File(filePath);
        FileCopyUtils.copy(multipartFile.getBytes(), uploadFile);
        //將上傳的圖片同一存為jpg格式
        this.changJPG(uploadFile);
        //生成縮略圖
        this.changSmall(uploadFile);
    }
    
    private void changJPG(File uploadFile) throws IOException {
        String path = uploadFile.getPath();
        // TODO Auto-generated method stub
         BufferedImage bufferedImage= ImageIO.read(uploadFile);
         // create a blank, RGB, same width and height, and a white background
         BufferedImage newBufferedImage = new BufferedImage(bufferedImage.getWidth(),
               bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB);
        //TYPE_INT_RGB:創建一個RBG圖像,24位深度,成功將32位圖轉化成24位
         newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0, Color.WHITE, null);
         // write to jpeg file
         String fileName = path.substring(0,path.lastIndexOf("."));
         ImageIO.write(newBufferedImage, "jpg", new File(fileName+".jpg"));
         
    }

    @Override
    public void run() {
        try {
            upload();
        } catch (Exception e) {
            LOG.error("文件上傳失敗:", e);
        }
    }

    public String getFileUrl() {
        return fileUrl;
    }
    /**
     * 將指定圖片在指定 位置生成縮略圖
     */
    private void changSmall(File uploadFile){
           String path = uploadFile.getPath();
           try {
               BufferedImage input = ImageIO.read(uploadFile);
               BufferedImage inputbig = new BufferedImage(33, 33, BufferedImage.TYPE_INT_BGR);
               Graphics2D g = (Graphics2D) inputbig.getGraphics();
               g.drawImage(input, 0, 0,33,33,null); //畫圖
               g.dispose();
               inputbig.flush();
               String fname = path.substring(0, path.lastIndexOf("."));//新名字
               String parent = uploadFile.getParent();
               ImageIO.write(inputbig, "jpg", new File( fname + "_small.jpg")); //將其保存在C:/imageSort/targetPIC/下
           } catch (Exception ex) {
               ex.printStackTrace();
           }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM