我正在使用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。

如果您想发出PUT请求,只需将所有事情都作为POST请求进行,但将_method => PUT添加到您的form-data参数中。

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

需要传递一个文件来上传: 按照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')

成功了!

首先,设置post in方法和填充链接API

然后选择Body -> form-data ->输入你的参数名(根据你的代码文件)