如何在 Postman 中上传文件和 JSON 数据? [英] How to upload a file and JSON data in Postman?

查看:38
本文介绍了如何在 Postman 中上传文件和 JSON 数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Spring MVC,这是我的方法:

I am using Spring MVC and this is my method:

/**
* 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.我该怎么做?


I need to pass the session id in postman and also the file. How can I do that?

推荐答案

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

In postman, set method type to POST.

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

Then select Body -> form-data -> Enter your parameter name (file according to your code)

在值列的右侧,将有下拉文本,文件",选择文件.选择您的图像文件并发布.

and on right side next to value column, there will be dropdown "text, file", select File. choose your image file and post it.

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

For rest of "text" based parameters, you can post it like normally you do with postman. Just enter parameter name and select "text" from that right side dropdown menu and enter any value for it, hit send button. Your controller method should get called.

这篇关于如何在 Postman 中上传文件和 JSON 数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆