我正在使用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)。 现在改变搜索内容类型,在黄色标记的区域。

在我的例子中:

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

其他回答

在弹簧架侧使用以下代码:

@PostMapping(value = Constant.API_INITIAL + "/uploadFile")
    public UploadFileResponse uploadFile(@RequestParam("file") MultipartFile file,String jsonFileVo) {
        FileUploadVo fileUploadVo = null;
        try {
            fileUploadVo = new ObjectMapper().readValue(jsonFileVo, FileUploadVo.class);
        } catch (Exception e) {
            e.printStackTrace();
        }

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

以下是步骤:

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

在我的例子中:

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

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

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

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

缺失的视觉指南

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

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