栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

上传文件到项目文件夹下

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

上传文件到项目文件夹下

package com.lpssy.tool;

import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;


public class UpFileutil {
    
    public static String UpImg(MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return "文件不能为空";
        }else {
            //文件原名称
            String fileName = file.getOriginalFilename();
            String[] split = fileName.split("\.");
            String FilesName = split[0];
            String trueFileName = null;
            //文件类型
            String type = fileName.contains(".") ? fileName.substring(fileName.lastIndexOf(".") + 1) : null;
            if (type == null) {
                return "文件类型为空";
            } else if ("gif".equals(type) || "png".equals(type) || "jpg".equals(type)||"GIF".equals(type) || "PNG".equals(type) || "JPG".equals(type)) {
            //自定义的文件名称,避免文件名重复
            trueFileName = "Img-"+ FilesName +"-"+ String.valueOf(System.currentTimeMillis()) + "." + type;
            String filePath = "C:/Users/Axx/Desktop/毕业设计+吴肖/YM/IEquipmentRental/src/main/webapp/WEB-INF/equipmentImg/";
            File fileDir =  new File(filePath);
            File newFile = new File(fileDir.getAbsolutePath() + "/" + trueFileName);
            file.transferTo(newFile);
            }else if("mp4".equals(type)||"MP4".equals(type)){
                //自定义的文件名称,避免文件名重复
                trueFileName = "video-"+ FilesName +"-"+ String.valueOf(System.currentTimeMillis()) + "." + type;
                String filePath = "C:/Users/Axx/Desktop/毕业设计+吴肖/YM/IEquipmentRental/src/main/webapp/WEB-INF/equipmentVideo/";
                File fileDir =  new File(filePath);
                File newFile = new File(fileDir.getAbsolutePath() + "/" + trueFileName);
                file.transferTo(newFile);
            }
            return trueFileName;
        }

    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/572775.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号