• 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();
        }
    }
  • 相关阅读:
    贡献15本经典C、C++、MFC、VC++教程,都是pdf完整版的
    雪花
    孙鑫C++视频教程 rmvb格式 全20CD完整版 精品分享
    mac上用VMWare虚拟机装win7
    阿里云如何解析域名,搭建云服务器环境
    2. Windows编程基础
    复制指定目录下的全部文件到另一个目录中
    Select查询命令
    使用OneNote2016发送博客
    Linux数字雨
  • 原文地址:https://www.cnblogs.com/acm-bingzi/p/springboot_upload_file.html
Copyright © 2020-2023  润新知