Springboot+HTML5+Layui2.7.6上传文件【请求上传接口出现异常】

1.最近两天在springboot+html5项目中发现在用layui框架时报请求上传接口出现异常这个错误。

2.将代码全部整理了一遍,发现前端后台都没错!!!

但是还是【请求上传接口出现异常】,于是跑去翻看layui官网。

 

3.最终最终将错误锁定到了返回的JSON字符串中,我是返回的String,所以一直都会报【请求上传接口出现异常】。

 4.可以在controller中将其返回成JSON字符串。

<1>首先我们要导入一个依赖

<!-- fast json--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.54</version> </dependency>

<2>在controller中修改成上传成功返回JSON字符串

@PostMapping("/uploadFile") @ResponseBody public HashMap<String, Object> singleFileUpload(@RequestParam("file") MultipartFile file) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("fileName = {}", file.getOriginalFilename()); } try { if (file == null || NULL_FILE.equals(file.getOriginalFilename())) { // return "upload failure"; return ResultMapUtil.getHashMapUploadFile("upload failure","500"); } fileService.saveFile(file.getBytes(), uploadFolder, file.getOriginalFilename()); } catch (Exception e) { // return "upload failure"; return ResultMapUtil.getHashMapUploadFile("upload failure","500"); } // return "upload success"; return ResultMapUtil.getHashMapUploadFile("upload success","200"); }

【注意:这一步中不同的人写的项目不同,controller也不相同,需要自己的根据实际情况来】

<3>给前端返回JSON格式数据

/** * 给前端返回的json格式数据 */ public class ResultMapUtil { // 上传文件返回结果 public static HashMap<String,Object> getHashMapUpload(String msg, String code){ HashMap<String,Object> resultMap = new HashMap<>(); resultMap.put("msg",msg); resultMap.put("code",code); if("200".equals(code)){ resultMap.put("msg","success"); }else { resultMap.put("msg","failure"); } return resultMap; } }

<4>文件上传service文件

@Service @Transactional public class FileServiceImpl implements FileService { private static final String UTF_8 = "UTF-8"; @Value("${file.uploadFolder}") private String uploadFolder; @Override public void saveFile(byte[] file, String filePath, String fileName) throws Exception { File targetFile = new File(filePath); if (!targetFile.exists()) { targetFile.mkdirs(); } FileOutputStream out = new FileOutputStream(filePath + fileName); out.write(file); out.flush(); out.close(); } }

<5>文件上传service实现类文件

@Service @Transactional public class FileServiceImpl implements FileService { private static final String UTF_8 = "UTF-8"; @Value("${file.uploadFolder}") private String uploadFolder; @Override public void saveFile(byte[] file, String filePath, String fileName) throws Exception { File targetFile = new File(filePath); if (!targetFile.exists()) { targetFile.mkdirs(); } FileOutputStream out = new FileOutputStream(filePath + fileName); out.write(file); out.flush(); out.close(); } }

5.上传文件的接口按照自己的来,这一步就已经可以成功上传并不会显示请求上传接口出现异常

6.【写在最后】,我们都在路上,加油!!!


__EOF__

本文作者aqdm-liuliu
本文链接https://www.cnblogs.com/aqdm-liuliu/p/17293836.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   爱学习的刘刘^  阅读(540)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示