用于文件上传和 JSON 数据的 Rest 服务 Java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28343119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rest service Java for file upload and JSON data
提问by Anand
Can I have a rest service that can be used for file upload i.e. multi-part form data and JSON parameter? Below is the example of the service.
我可以有一个可用于文件上传的休息服务,即多部分表单数据和 JSON 参数吗?以下是该服务的示例。
@POST
@Path("/upload")
@Consumes({ MediaType.MULTIPART_FORM_DATA, MediaType.APPLICATION_JSON })
public Response uploadFile(@FormDataParam("file") InputStream uploadedInputStream,@FormDataParam("file") FormDataContentDisposition fileDetail, City city){
The problem is while testing I am trying to pass both file as an attachment and city object as JSON, it is giving me error as Content-Type could either be application/json
or multipart/form-data
.
问题是在测试时我试图将文件作为附件传递,城市对象作为 JSON 传递,它给我错误,因为 Content-Type 可能是application/json
或multipart/form-data
.
Let me know if there is any way to handle this
让我知道是否有任何方法可以处理此问题
回答by Muhammad Sadiq
You may Use Any Client Side Language to submit form with MultipartFile and Json data. I am writing Java Code in Spring MVC here. It will send String Json and MultiPartFile. then Me going to to Cast String JSON to Map, and Save File at Desired Location.
您可以使用任何客户端语言来提交带有 MultipartFile 和 Json 数据的表单。我在这里在 Spring MVC 中编写 Java 代码。它将发送 String Json 和 MultiPartFile。然后我打算将字符串 JSON 转换为映射,并将文件保存在所需位置。
@RequestMapping(value="/hotel-save-update", method=RequestMethod.POST )
public @ResponseBody Map<String,Object> postFile(@RequestParam(value="file", required = false) MultipartFile file,
@RequestParam(value = "data") String object ){
Map<String,Object> map = new HashMap<String, Object>();
try {
ObjectMapper mapper = new ObjectMapper();
map = mapper.readValue(object, new TypeReference<Map<String, String>>(){});
}catch (Exception ex){
ex.printStackTrace();
}
String fileName = null;
if (file != null && !file.isEmpty()) {
try {
fileName = file.getOriginalFilename();
FileCopyUtils.copy(file.getBytes(), new FileOutputStream(servletContext.getRealPath("/resources/assets/images/hotelImages") + "/" + fileName));
} catch (Exception e) {
header.put(Utils.MESSAGE, "Image not uploaded! Exception occured!");
return result;
}
}
}
}
回答by Richard
Can't you leave the @Consumes off and check the Content-Type header in the method itself, deciding what to do in code? Your problem seems to be a restriction in the functionality of that annotation (is it Spring MVC?)
您不能关闭 @Consumes 并检查方法本身中的 Content-Type 标头,决定在代码中做什么吗?您的问题似乎是该注释功能的限制(它是 Spring MVC 吗?)
回答by Anand
I have solved my problem by passing JSON as String from client and then converting String to JSON object.
我通过从客户端将 JSON 作为 String 传递然后将 String 转换为 JSON 对象来解决我的问题。
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(@FormDataParam("file") InputStream uploadedInputStream,
@FormDataParam("file") FormDataContentDisposition fileDetail, @FormDataParam("city") String city){