SpringBoot文件上传到本地文件夹

SpringBoot文件上传到本地文件夹

最近项目中做到了页面上传文件至本地文件夹功能,并且可以下载查看的小功能特此记录

1、前端部分介绍
项目中使用的前端是Vue + IView样式框架,IView效果图如下
在这里插入图片描述

IView文件上传
查看部分只要用a标签,将给href属性赋值为我们下面保存的文件路径即可

<a :href="row.url" :disabled="row.sfys !== '1'" target="_blank">查看</a>

页面代码就不贴了哈

Ajax部分

// 开始上传
          uploadFJ() {
            this.loading = true;
            let form = new FormData();
            // 文件对象
            form.append("file", this.file); // 该参数是文件对象
            form.append("id", this.id);  // 唯一标识用于存储更新表中地址
            $.ajax({
              type: "POST",
              url: api.uploadFj,
              data: form,
              //ajax2.0可以不用设置请求头,但是jq帮我们自动设置了,这样的话需要我们自己取消掉
              contentType:false,
              //取消帮我们格式化数据,是什么就是什么
              processData:false,
              success: res => {
                if (res.code === undefined) {
                  this.$Message.success('上传成功!');
                } else if (res.code === 500) {
                  this.$Message.error('操作繁忙!');
                }
              }
            })
          },

2、后台部分

	/**
     * Description: 上传文件
     * Author: WangConvey
     * Date: 2021/4/19/019 16:06
     * Param: [file, id]
     * Return: java.lang.String
     */
    @RequestMapping("/uploadFj")
    @SkipAuthentication
    public String uploadFj(MultipartFile file, String id) {
        try {
            log.info("/zyk/updateFj 调用成功");
            zykService.uploadFj(file, id);
            return ok("ok");
        } catch (Exception e) {
            e.printStackTrace();
            log.warn("/zyk/updateFj 异常:" + e.getMessage());
            return error(500, e.getMessage());
        }
    }

Service部分

	@Value("${app.fileStorePath}")
    private String filePath;
	/**
     * Description: 上传文件
     * Author: WangConvey
     * Date: 2021/4/19/019 16:17
     * Param: [file, id,]
     * Return: void
     */
    public void uploadFj(MultipartFile uploadFile, String id) throws IOException {
        String tempPath = filePath;
        File file = new File(tempPath);
        // Temp文件夹是否存在
        if (!file.exists()) {
            file.mkdir();
        }
        String path = tempPath + "/" + uploadFile.getOriginalFilename();
        file = new File(path);
        // 保存文件
        uploadFile.transferTo(file);
        // 下面是将文件url保存在地址字段中
        FJCL fjcl = new FJCL();
        fjcl.setId(id);
        fjcl.setSfys("1");
        // localPath是下面部分配置的静态资源映射部分
        String url = "/localPath/" + uploadFile.getOriginalFilename();
        fjcl.setUrl(url);
        fjclDao.updateById(fjcl);
    }

配置文件

app:
  fileStorePath: D:/Temp #文件存储根目录

因为SpringBoot对静态资源保护,也就是说直接在项目中访问本地文件是不被允许的,所以添加要放行配置
配置部分

/**
 * Description: 静态资源放行
 * Author: WangConvey
 * Date: 2021/4/19/019 17:27
 * Version: 1.0
 */
@Component
public class WebMvcConfig implements WebMvcConfigurer {

    @Value("${app.fileStorePath}")
    private String filePath;

    /**
     * springboot 无法直接访问静态资源,需要放开资源访问路径。
     * 添加静态资源文件,外部可以直接访问地址
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
		// 意思是我们通过项目访问资源路径为/localPath开头的将会被映射到D:/Temp下
        registry.addResourceHandler("/localPath/**").addResourceLocations("file:///" + filePath);
    }
}

至此结束,感谢各位阅读,欢迎评论指正

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值