java更改图片格式,,更改图片大小,并实现多线程上传图片。

Posted Roomed

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();
           }
    }
}

 

以上是关于java更改图片格式,,更改图片大小,并实现多线程上传图片。的主要内容,如果未能解决你的问题,请参考以下文章

kindeditor批量上传,怎么更改一次性上传图片的数量和图片大小限制呢?

java 如何使用多线程调用类的静态方法?

最佳文件上传方法

android:radiobutton的大小可以更改,你是怎么实现的

png图片太大怎么压缩(png图片怎么压缩大小)

多线程下载图片并压缩