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


当前回答

如果你想把Id和File放在一个对象中,你可以把请求对象添加到一个标准的方法中,然后在Postman中设置Body为form-data,并在键前加上请求对象的名称。如请求。SessionId和request.File。

其他回答

发送包含json数据文件的多部分数据的方法如下,我们需要在邮递员body选项卡中设置相应json关键字段的content-type为'application/json',如下所示:

在看到所有的答案后,我感到困惑,我找不到任何合适的截图来带来内容类型列。过了一段时间,我自己找到了它。希望这能帮助到像我这样的人。

以下是步骤:

点击邮递员的红色标记区域。 现在检查绿色标记的选项(Content Type)。 现在改变搜索内容类型,在黄色标记的区域。

在我的例子中:

Invoice_id_ls (key)包含json数据。 文档包含文件数据。 Placed_amount包含普通文本字符串。

在[SHOW COLUMNS]中选择[Content Type],然后将“application/json”的Content - Type设置为json text参数。

对于每个可以设置Content-Type的表单数据键,右侧有一个邮递员按钮来添加Content-Type列,并且您不必从控制器中的字符串解析json。

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

邮差:

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

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