尝试使用邮递员上传 MultipartFile

编程入门 行业动态 更新时间:2024-10-24 12:20:42
本文介绍了尝试使用邮递员上传 MultipartFile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用 PostMan 上传 Multipart 文件并出现错误.下面是代码和截图:

I am trying to upload a Multipart File using PostMan and getting errors. Here is the code and screenshots:

imgur/pZ5cXrh

imgur/NaWQceO

@RequestMapping(value = "/upload", method = RequestMethod.POST) public void uploadFileHandler(@RequestParam("name") String name, @RequestParam("name") MultipartFile file) { if (!file.isEmpty()) { try { byte[] bytes = file.getBytes(); // Creating the directory to store file //String rootPath = System.getProperty("catalina.home"); String rootPath = "C:\Desktop\uploads"; File dir = new File(rootPath + File.separator + "tmpFiles"); if (!dir.exists()) dir.mkdirs(); // Create the file on server File serverFile = new File(dir.getAbsolutePath() + File.separator + name); BufferedOutputStream stream = new BufferedOutputStream( new FileOutputStream(serverFile)); stream.write(bytes); stream.close(); System.out.println("Server File Location=" + serverFile.getAbsolutePath()); System.out.println("You successfully uploaded file=" + name); } catch (Exception e) { System.out.println("You failed to upload " + name + " => " + e.getMessage()); } } else { System.out.println("You failed to upload " + name + " because the file was empty."); } }

推荐答案

你应该有这样的东西:

@RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = "multipart/form-data") public void uploadFileHandler(@RequestParam("name") String name, @RequestParam("file") MultipartFile file) { if (!file.isEmpty()) { try { byte[] bytes = file.getBytes(); // Creating the directory to store file //String rootPath = System.getProperty("catalina.home"); String rootPath = "C:\Users\mworkman02\Desktop\uploads"; File dir = new File(rootPath + File.separator + "tmpFiles"); if (!dir.exists()) dir.mkdirs(); // Create the file on server File serverFile = new File(dir.getAbsolutePath() + File.separator + name); BufferedOutputStream stream = new BufferedOutputStream( new FileOutputStream(serverFile)); stream.write(bytes); stream.close(); System.out.println("Server File Location=" + serverFile.getAbsolutePath()); System.out.println("You successfully uploaded file=" + name); } catch (Exception e) { System.out.println("You failed to upload " + name + " => " + e.getMessage()); } } else { System.out.println("You failed to upload " + name + " because the file was empty."); } }

请注意consumes = "multipart/form-data".这是您上传的文件所必需的,因为您应该进行多部分通话.你应该有 @RequestParam("file") MultipartFile file 而不是 @RequestParam("name") MultipartFile file).

Please pay attention to consumes = "multipart/form-data". It is necessary for your uploaded file because you should have a multipart call. You should have @RequestParam("file") MultipartFile file instead of @RequestParam("name") MultipartFile file).

当然,您应该配置一个 multipartview 解析器,内置对 apache-commons 文件上传和原生 servlet 3 的支持.

Of course you should have configured a multipartview resolver the built-in support for apache-commons file upload and native servlet 3.

更多推荐

尝试使用邮递员上传 MultipartFile

本文发布于:2023-10-29 04:02:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538668.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:邮递员   上传   MultipartFile

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!