SpringBoot上传文件到本服务器 目录与jar包同级

工具类涉及pom依赖包

<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>

工具类

/**
 * SpringBoot上传文件工具类
 * @author LinkinStar
 */
public class FileHandleUtil {

    /** 绝对路径 **/
    private static String absolutePath = "";
    
    /** 静态目录 **/
    private static String staticDir = "static";
    
    /** 文件存放的目录 **/
    private static String fileDir = "/upload/";

    /** 
     * 上传单个文件
     * 最后文件存放路径为:static/upload/image/test.jpg
     * 文件访问路径为:http://127.0.0.1:8080/upload/image/test.jpg
     * 该方法返回值为:/upload/image/test.jpg
     * @param inputStream 文件流
     * @param path 文件路径,如:image/
     * @param filename 文件名,如:test.jpg
     * @return 成功:上传后的文件访问路径,失败返回:null
     */
    public static String upload(InputStream inputStream, String path, String filename) {
        //第一次会创建文件夹
        createDirIfNotExists();

        String resultPath = fileDir + path + filename; 
        
        //存文件
        File uploadFile = new File(absolutePath, staticDir + resultPath);
        try {
            FileUtils.copyInputStreamToFile(inputStream, uploadFile);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        
        return resultPath;
    }

    /**
     * 创建文件夹路径
     */
    private static void createDirIfNotExists() {
        if (!absolutePath.isEmpty()) {return;}
        
        //获取跟目录
        File file = null;
        try {
            file = new File(ResourceUtils.getURL("classpath:").getPath());
        } catch (FileNotFoundException e) {
            throw new RuntimeException("获取根目录失败,无法创建上传目录!");
        }
        if(!file.exists()) {
            file = new File("");
        }

        absolutePath = file.getAbsolutePath();
        
        File upload = new File(absolutePath, staticDir + fileDir);
        if(!upload.exists()) {
            upload.mkdirs();
        }
    }

    /**
     * 删除文件
     * @param path 文件访问的路径upload开始 如: /upload/image/test.jpg
     * @return true 删除成功; false 删除失败
     */
    public static boolean delete(String path) {
        File file = new File(absolutePath, staticDir + path);
        if (file.exists()) {
            return file.delete();
        }
        return false;
    }
}
@RequestMapping("/upload")
    @ResponseBody
    public ResponseData upload(HttpServletRequest requet, @ModelAttribute MultipartFile file ) throws IllegalStateException, IOException {

        if(!file.isEmpty()) {
            String result = requet.getScheme()+"://"+requet.getServerName()+":"+requet.getServerPort() + FileHandleUtil.upload(file.getInputStream(), "file/", file.getOriginalFilename());
            return new ResponseData("0", "上传成功", result);
        }else {
            return new ResponseData("1", "上传失败", null);
        }

    }

application.properties 文件记得加上如下配置,使jar包运行时能正常访问到上传文件。

spring.resources.static-locations=classpath:static/,file:static/

一、本地项目编译运行,上传后位置如图:
文件访问路径: http://localhost:8099/upload/file/数据共享管理办法_初稿.pdf
在这里插入图片描述
在这里插入图片描述

二、jar包运行,上传文件位置如下图:
在这里插入图片描述
文件访问路径: http://localhost:8099/upload/file/数据共享管理办法_初稿.pdf

参考自:https://msd.misuland.com/pd/8900562867645553

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值