码迷,mamicode.com
首页 > 编程语言 > 详细

springboot图片上传工具类

时间:2018-12-13 23:32:48      阅读:425      评论:0      收藏:0      [点我收藏+]

标签:img   sub   名称   source   java   sts   work   apache   abs   

package com.wiscom.ism.webapi.ismUtil;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;
@Component
public class FileUploadUtil {

    //存储路径
    public String getPath(){
        File path = null;
        try {
            path = new File(ResourceUtils.getURL("classpath:").getPath());
            if (!path.exists()){
                path.mkdirs();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        String imagesPath = path.getAbsolutePath();
        return imagesPath;
    }
    //上传存到硬盘
    public boolean upLoadImage(String imagesPath,String pathImg,InputStream fileInputStream){
        File file = new File(imagesPath,"static/"+pathImg);
        try {
            if (fileInputStream != null){
                FileUtils.copyInputStreamToFile(fileInputStream, file);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    //生成名字
    public String imageName(String image){
        String imageName =null;
        //生成uuid作为文件名称
        String uuid = UUID.randomUUID().toString().replaceAll("-","").substring(0,10);
        //后缀
        String imgType =image.substring(image.lastIndexOf(".")+1,image.length());
        if ("gif".equals(imgType) || "jpg".equals(imgType) || "png".equals(imgType)){
            imageName=uuid+"."+imgType;
        }
        return imageName;
    }
}

 

springboot图片上传工具类

标签:img   sub   名称   source   java   sts   work   apache   abs   

原文地址:https://www.cnblogs.com/maocai2018/p/10117024.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!