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


当前回答

缺失的视觉指南

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

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

其他回答

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

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

通过邮递员上传文件以及传递一些输入数据的步骤在下面的博客以及截图中进行了很好的讨论。在本博客中,api代码是用node js编写的。你可以再看一遍,看得更清楚些。

https://jksnu.blogspot.com/2021/09/how-to-create-post-request-with.html

缺失的视觉指南

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

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

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

请按照下图从上到下的步骤进行操作。

在第三步,你会发现下拉类型选择如下图所示