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


当前回答

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

其他回答

如果有人需要:

Body -> form-data

将字段名添加为数组

如果您使用cookie来保持会话,则可以使用拦截器将cookie从浏览器共享给邮递员。

也可以上传一个文件,你可以使用form-data标签下的正文标签邮递员,在其中,你可以提供数据在键值格式和每个键,你可以选择值文本/文件的类型。当你选择文件类型时,出现上传文件的选项。

Body > binary >选择文件

缺失的视觉指南

您必须首先找到几乎看不见的“文件”的浅灰色下拉菜单,这是解锁“选择文件”按钮的魔法键。

在你选择POST后,然后选择Body->form-data,然后找到File下拉菜单,然后选择File,然后才会神奇地出现choose Files按钮:

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