小编典典

如何在Postman中上传文件和JSON数据?

spring-mvc

我正在使用Spring MVC,这是我的方法:

/**
* Upload single file using Spring Controller.
*/
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public @ResponseBody ResponseEntity<GenericResponseVO<? extends IServiceVO>> uploadFileHandler(
            @RequestParam("name") String name,
            @RequestParam("file") MultipartFile file,
            HttpServletRequest request,
            HttpServletResponse response) {

    if (!file.isEmpty()) {
        try {
            byte[] bytes = file.getBytes();

            // Creating the directory to store file
            String rootPath = System.getProperty("catalina.home");
            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());

            return null;
        } catch (Exception e) {
            return null;
        }
    }
}

我需要在邮递员和文件中传递会话ID。我怎样才能做到这一点?


阅读 1277

收藏
2020-06-01

共1个答案

小编典典

在邮递员中,将方法类型设置为 POST

然后选择主体->表单数据->输入参数名称(根据您的代码 文件

且右侧旁边值列,会出现 下拉菜单“文本文件” ,选择 文件 。选择您的图像文件并将其发布。

对于其他基于 “文本”的参数
,您可以像通常使用postman一样进行发布。只需输入参数名称,然后从该右侧的下拉菜单中选择“文本”,然后为其输入任何值,请点击发送按钮。您的控制器方法应被调用。

2020-06-01