Har*_*.N. 82 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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我需要在邮递员和文件中传递会话ID.我怎样才能做到这一点?
Sum*_*aya 117
在postman中,将方法类型设置为POST.
然后选择Body - > form-data - >输入您的参数名称(根据您的代码提供的文件)
在值列旁边的右侧,会有下拉列表"text,file",选择File.选择您的图像文件并发布.
对于其余的基于"文本"的参数,您可以像通常使用邮递员一样发布.只需输入参数名称并从右侧下拉菜单中选择"文本",然后输入任意值,点击发送按钮.应调用您的控制器方法.
Chr*_*oll 45
我认为我们在这里缺少的视觉指南是,突出了File的雅致但几乎看不见的浅灰色白色下拉列表.
之后您选择File,然后选择Choose Files,然后找到下拉菜单,然后再选择"文件",只有这样,"选择文件"按钮,奇迹般地出现:
gce*_*gce 42
也许你可以这样做:
bur*_*gul 15
像这样 :
正文 - >表单数据 - >选择文件
你必须写"文件"而不是"名字"
您也可以从Body - > raw字段发送JSON数据.(只需粘贴JSON字符串)
小智 12
如果有人想以表单数据格式发送json数据只需要声明这样的变量
邮差:

如您所见,描述参数将采用基本的 json 格式,其结果是:
{ description: { spanish: 'hola', english: 'hello' } }
Run Code Online (Sandbox Code Playgroud)
在弹簧休息侧使用以下代码:
@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();
}
Run Code Online (Sandbox Code Playgroud)
看到所有答案后我感到困惑,我找不到任何合适的屏幕截图来显示该Content Type专栏。过了一段时间,我自己找到了。希望这会帮助像我这样的人。
以下是步骤:
Content Type)。就我而言:
invoice_id_ls(key) 包含json数据。documents包含file数据。placed_amount包含normal text字符串。小智 6
如果您需要使用表单数据分段上传文件并在同一POST请求中发送json数据(Dto对象)
在Controller中将JSON对象作为String获取,并通过添加以下行使其反序列化
ContactDto contactDto = new ObjectMapper().readValue(yourJSONString, ContactDto.class);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
185771 次 |
| 最近记录: |