如何在邮递员中上传文件和json数据 [英] how to upload a file and json data in postman

查看:112
本文介绍了如何在邮递员中上传文件和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?

推荐答案

在邮递员中,将方法类型设置为 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.

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

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