java - 如何在 Postman 中上传文件和 JSON 数据?

标签 java json spring-mvc postman

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


我需要在 postman 和文件中传递 session ID。我该怎么做?

最佳答案

在 postman 中,将方法类型设置为 POST

然后选择 正文 -> 表单数据 -> 输入您的参数名称(文件根据您的代码)

在 Key 字段的右侧,将鼠标悬停在其上时,有一个 下拉菜单 可以在 Text/File 之间进行选择。选择文件,然后“选择文件”按钮将出现在值字段中。

对于其余的基于“文本”的参数,您可以像通常使用 postman 一样发布它。只需输入参数名称并从右侧下拉菜单中选择“文本”并为其输入任何值,点击发送按钮。你的 Controller 方法应该被调用。

关于java - 如何在 Postman 中上传文件和 JSON 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39037049/

相关文章:

java - 无法理解Calendar类的before函数

java - 仅当特定 header 存在时,Spring RepositoryRestResource 响应

javascript - 带有 fs 和 setInterval 的 Node js

java - Spring 表单绑定(bind)到 c 内的 Set :forEach

spring - 在类路径上未检测到 Spring WebApplicationInitializer 类型

java - Spring mvc 从 formBackingObject() 重定向到一个页面

java - 使用 Apache commons HttpClient 时如何覆盖请求中的 "Host" header

java - 禁用 PathVariables 的假编码

json - 使用 JQ 合并一个文件中的两个 JSON 片段

java - 没有 POJOS 并在 rutime 中构建复合比较器以进行多级排序