springboot前端上传文件后端接收不到

后端代码:

import org.springframework.web.multipart.MultipartFile;
/**
     * 文件上传
     * @param file 上传的excel文件
     * @return
     * @throws Exception
     */
    @PostMapping("/uploadExcelItems")
    @CrossOrigin(allowCredentials = "true", origins = {"*"})
    @ResponseBody
    public ReturnResult<?> uploadExcelItems(@RequestParam("file") MultipartFile file) throws  Exception{
    if (file == null) {
	            return new ReturnResult<>().failed(ReturnCodeEnum.PARAM_ERROR.getCode(),ReturnCodeEnum.PARAM_ERROR.getDesc(),"上传文件不能为空");
	}
 }

前端反馈:
在这里插入图片描述
在这里插入图片描述
最终发现:
name="excel"改为name=“file” 问题解决

Spring Boot后端中,可以使用Java的File类来操作文件,读取文件内容并将其封装为对象传递到前端。 首先,需要定义一个Java类来表示文件对象,可以包含文件名、文件路径、文件内容等属性。例如: ``` public class FileObject { private String fileName; private String filePath; private String fileContent; // getter and setter methods } ``` 然后,在后端中使用File类来读取文件内容,并将其封装为FileObject对象。可以使用以下代码: ``` File file = new File(filePath); // filePath为文件路径 String fileContent = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8); // 读取文件内容 FileObject fileObject = new FileObject(); fileObject.setFileName(file.getName()); fileObject.setFilePath(filePath); fileObject.setFileContent(fileContent); ``` 最后,将FileObject对象传递到前端。可以使用Spring Boot的RestController注解来定义一个RESTful接口,将FileObject对象转换为JSON格式并返回给前端。例如: ``` @RestController public class FileController { @GetMapping("/file") public FileObject getFile() { File file = new File(filePath); // filePath为文件路径 String fileContent = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8); // 读取文件内容 FileObject fileObject = new FileObject(); fileObject.setFileName(file.getName()); fileObject.setFilePath(filePath); fileObject.setFileContent(fileContent); return fileObject; } } ``` 前端可以通过发送HTTP请求来调用这个接口,并获取FileObject对象。可以使用JavaScript的fetch API来发送GET请求并处理响应。例如: ``` fetch('/file') .then(response => response.json()) .then(fileObject => { console.log(fileObject); // 在页面中显示文件内容等信息 }); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我为offer而来

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值