zoukankan      html  css  js  c++  java
  • springboot图片上传工具类

    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;
        }
    }
  • 相关阅读:
    mysql通过data目录恢复数据库
    CentOS安装TortoiseSVN svn 客户端
    CentOS上安装Node.js
    PHP--进行模块化设计
    PHP开发绝对不能违背的安全铁则
    达内培训:php在线端口扫描器
    使用 PHP 限制下载速度
    使用无限生命期Session的方法
    使用php作linux自动执行脚本
    腾讯星座运势api
  • 原文地址:https://www.cnblogs.com/maocai2018/p/10117024.html
Copyright © 2011-2022 走看看