我正在使用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。我该怎么做呢?


当前回答

您可以同时发送Image和可选/必选参数。

在postman中,有Params标签。

其他回答

也许你可以这样做:

如果你需要 上传文件在多个部分使用表单数据和发送json数据(Dto对象)在同一个POST请求

在控制器中获取JSON对象作为字符串,并通过添加这一行使其反序列化

ContactDto contactDto  = new ObjectMapper().readValue(yourJSONString, ContactDto.class);

我需要传递两个:一个文件和一个整数。我是这样做的:

需要传递一个文件来上传: 按照Sumit的回答做了。 请求类型:POST Body -> form-data 在KEY标题下,输入变量的名称(后端代码中的'file')。 在后端: File = request.files[' File '] 在“文件”旁边,有一个下拉框,允许你在“文件”或“文本”之间进行选择。选择“File”,在VALUE标题下,出现了“Select files”。点击这个,打开一个窗口选择文件。

2. 需要传递一个整数:

去:

参数个数

在KEY下输入变量名(例如:id),在value下输入变量值(例如:1)

在后端:

id = request.args.get('id')

成功了!

如果有人需要:

Body -> form-data

将字段名添加为数组

如果有人想以form-data格式发送json数据,只需要像这样声明变量

邮差:

如你所见,description参数将是基本的json格式,结果是:

{ description: { spanish: 'hola', english: 'hello' } }