文件上传工具类

 yml文件配置文件上传路径:

#文件上传地址
uploadfileimgurl: /home/im_user/im_server/im_webserver/apache-tomcat/webapps/upload-image/

 工具类

package com.xs.wastesorting.utils;


import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 文件上传
 *
 * @description: hehsmk
 * @date: 2023/8/15
 */

public class UploadFileUtil {


    public static String uploadFile(MultipartFile multipartFile, String uploadPath) {
        //判断文件是否为空 isEmpty
        if (multipartFile == null) {
            return "文件为空";
        }
        //构建日期
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String dateTime = simpleDateFormat.format(date.getTime());
        //获取文件的原名称 getOriginalFilename
        String OriginalFilename = multipartFile.getOriginalFilename();
        //获取时间戳和文件的扩展名,拼接成一个全新的文件名; 用时间戳来命名是为了避免文件名冲突
        String fileName = System.currentTimeMillis() + "." + OriginalFilename.substring(OriginalFilename.lastIndexOf(".") + 1);
        //定义文件存放路径
        // String filePath = "D:\\Test\\";
        //新建一个目录(文件夹)
        String newpath = uploadPath + dateTime + "/" + fileName;
        File dest = new File(newpath);
        //判断filePath目录是否存在,如不存在,就新建一个
        if (!dest.getParentFile().canExecute()) {
            dest.getParentFile().mkdirs(); //新建一个目录
        }
        try {
            //文件输出
            multipartFile.transferTo(dest);
            dest.setReadable(true, false);
            dest.setExecutable(false);
        } catch (Exception e) {
            e.printStackTrace();
            //拷贝失败要有提示
            return "上传失败";
        }
        return newpath;

    }

    //上传路径存放配置文件
    public static String newUploadFile(MultipartFile multipartFile, String account, String uploadfileimgurl) {
        //判断文件是否为空 isEmpty
        if (multipartFile == null) {
            return "文件为空";
        }
        //构建日期
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String dateTime = simpleDateFormat.format(date.getTime());
        //获取文件的原名称 getOriginalFilename
        String OriginalFilename = multipartFile.getOriginalFilename();
        //获取时间戳和文件的扩展名,拼接成一个全新的文件名; 用时间戳来命名是为了避免文件名冲突
        String fileName = System.currentTimeMillis() + "." + OriginalFilename.substring(OriginalFilename.lastIndexOf(".") + 1);
        //定义文件存放路径
        // String filePath = "D:\\Test\\";
        //新建一个目录(文件夹)
        String newpath = uploadfileimgurl + account + "/" + dateTime + "/" + fileName;
        File dest = new File(newpath);
        //判断filePath目录是否存在,如不存在,就新建一个
        if (!dest.getParentFile().canExecute()) {
            dest.getParentFile().mkdirs(); //新建一个目录
        }
        try {
            //文件输出
            multipartFile.transferTo(dest);
            dest.setReadable(true, false);
            dest.setExecutable(false);
        } catch (Exception e) {
            e.printStackTrace();
            //拷贝失败要有提示
            return "上传失败";
        }
        // 从后向前查找第一个"/"的位置
        int firstIndex = newpath.lastIndexOf("/");
        while (firstIndex != -1 && !newpath.substring(firstIndex + 1).startsWith("upload-image")) {
            firstIndex = newpath.lastIndexOf("/", firstIndex - 1);
        }

// 截取该位置之后的子字符串
        String subPath = newpath.substring(firstIndex);
        return subPath;

    }
}

controller层:

package com.xs.wastesorting.controller;

import com.xs.wastesorting.dto.xsupload.Result;
import com.xs.wastesorting.entity.StreetTown;
import com.xs.wastesorting.utils.ExcelUtils;
import com.xs.wastesorting.utils.GetUserAccountUtil;
import com.xs.wastesorting.utils.UploadFileUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * 文件上传
 */
@RestController
@RequestMapping(value = "/api/xs/wastetype/file")
public class UploadFileController {

    @Value("${uploadfileimgurl}")
    private String uploadfileimgurl;

    @Resource
    private HttpServletRequest httpServletRequest;


    /**
     * 文件上传
     *
     * @param file
     * @return
     */
    @PostMapping(value = "/uploadfile")
    public Result uploadFile(@RequestParam("file") MultipartFile file) {
        String account = GetUserAccountUtil.getAccount(httpServletRequest);
        String fileurl = UploadFileUtil.newUploadFile(file, account, uploadfileimgurl);
        return Result.success(fileurl);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值