springboot+vue 前后端分离使用axios上传文件

参考博客:https://www.cnblogs.com/lenve/p/10782774.html

vue代码

<template>
    <input type="file" ref="myfile">
    <el-button @click="importData" type="success" size="mini">导入数据</el-button>
</template>
<script>
import axios from 'axios';
//统一设置axios请求上的Credentials
axios.defaults.withCredentials = true;
export default {
    components:{
        axios
    },
    methods:{
        importData() {
            let myfile = this.$refs.myfile;
            let files = myfile.files;
            let file = files[0];
            var formData = new FormData();
            formData.append("file", file);
            axios({
                method: 'post',
                url: 'http://localhost:10010/yp/upload',//跳转路径
                data: formData,
                headers: {
                 'Content-Type': 'multipart/form-data'
                }
            });
        },
    }
}
    
</script>

java代码

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
@RestController
public class AddFileMapper {
    SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");
    DecimalFormat    df   = new DecimalFormat("######0.00");
    @RequestMapping("/yp/upload")
    public String importData(MultipartFile file, HttpServletRequest req) throws IOException {
        try{
            String format = sdf.format(new Date());
            //路径方式:1、绝对路径
            String realPath = "F:/S3Project/vue_project/src/assets/upload"+format;
            //路径方式:2、相对路径
            //String realPath = req.getServletContext().getRealPath("/upload") + format;
            System.out.println("存放路径:"+realPath);
            File folder = new File(realPath);
            if (!folder.exists()) {
                folder.mkdirs();
            }
            String oldName = file.getOriginalFilename();
            String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."));
            file.transferTo(new File(folder,newName));
            String url = "http://localhost:3000/src/assets/upload" + format + newName;
            //String url = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + "/upload" + format + newName;
            System.out.println("前端访问路径:"+url);
            //文件名称
            String fileName="/upload" + format + newName;
            //文件大小
            String fileSize = df.format((file.getSize()/1024))+"kb";
            System.out.println("文件名称:"+fileName+"---文件大小"+fileSize);
        }catch (Exception e){
            return "上传失败";
        }
        return "上传成功";
    }
}

配置文件

上传的文件大小默认是1MB,我们可以在application.yaml里设置上传的文件大小限制

  spring:
	  servlet:
	    multipart:
	      max-file-size: 10MB
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值