欢迎访问我的个人网站==》 jiashubing.cn

springboot上传文件到本地服务器

上传到本地服务器下,不需要修改配置文件

在本地开发测试模式时,得到的地址为: {项目跟目录}/target/static/images/upload/

在打包成jar发布到服务器上时,得到的地址为: {发布jar包目录}/static/images/upload/

package cn.jiashubing.controller;

import cn.jiashubing.common.StringConstants;
import cn.jiashubing.result.ResultModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * @author jiashubing
 * @since 2019/6/25
 */
@RestController
@Api(tags = "稿件")
@RequestMapping(value = StringConstants.API)
public class ArticleController {

    @ApiOperation("上传附件")
    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
    public ResultModel<String> uploadFile(@RequestParam("file") @ApiParam(value = "二进制文件流") MultipartFile file) {
        String fileName = file.getOriginalFilename();//获取文件名
        fileName = getFileName(fileName);
        String filepath = getUploadPath();
        if (!file.isEmpty()) {
            try (BufferedOutputStream out = new BufferedOutputStream(
                    new FileOutputStream(new File(filepath + File.separator + fileName)))) {
                out.write(file.getBytes());
                out.flush();
                return new ResultModel<>(fileName);
            } catch (FileNotFoundException e) {
                return new ResultModel<>(false, "上传文件失败 FileNotFoundException:" + e.getMessage());
            } catch (IOException e) {
                return new ResultModel<>(false, "上传文件失败 IOException:" + e.getMessage());
            }
        } else {
            return new ResultModel<>(false, "上传文件失败,文件为空");
        }
    }

    /**
     * 文件名后缀前添加一个时间戳
     */
    private String getFileName(String fileName) {
        int index = fileName.lastIndexOf(".");
        final SimpleDateFormat sDateFormate = new SimpleDateFormat("yyyymmddHHmmss");  //设置时间格式
        String nowTimeStr = sDateFormate.format(new Date()); // 当前时间
        fileName = fileName.substring(0, index) + "_" + nowTimeStr + fileName.substring(index);
        return fileName;
    }

    /**
     * 获取当前系统路径
     */
    private String getUploadPath() {
        File path = null;
        try {
            path = new File(ResourceUtils.getURL("classpath:").getPath());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        if (!path.exists()) path = new File("");
        File upload = new File(path.getAbsolutePath(), "static/upload/");
        if (!upload.exists()) upload.mkdirs();
        return upload.getAbsolutePath();
    }
}

 

posted @ 2020-03-19 16:43  贾树丙  阅读(13476)  评论(1编辑  收藏  举报