gpt4 book ai didi

java - ClientHandlerException : MIME media type, 多部分/表单数据,未找到

转载 作者:太空宇宙 更新时间:2023-11-04 10:24:05 25 4
gpt4 key购买 nike

我正在使用 Jersey 客户端来访问 Spring MVC REST Controller 以实现图像上传功能。我收到以下异常:

com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class org.springframework.web.multipart.commons.CommonsMultipartFile, and MIME media type, multipart/form-data, was not found

我的 Controller 方法来发布图像:

@RequestMapping(value = "/file/upload", method = RequestMethod.POST)
public String fileUpload(@RequestParam("fileUpload") MultipartFile file,
Model model, HttpServletRequest request, HttpServletResponse response)
{
try
{
ClientConfig config = new DefaultClientConfig();
com.sun.jersey.api.client.Client client = com.sun.jersey.api.client.Client.create(config);
WebResource webResource = client.resource("/save-image");

ClientResponse responseMsg = webResource
.type(MediaType.MULTIPART_FORM_DATA)
.post(ClientResponse.class, file);
}
catch (Exception e)
{
logger.error("Exception in fileUpload()", e);
return "error";
}
return "success";
}

我的 REST Controller 方法获取发布数据:

@ResponseBody
@Consumes(MediaType.MULTIPART_FORM_DATA)
@RequestMapping(value = "/save-image", method = RequestMethod.POST)
public String saveImage(@FormDataParam("file") MultipartFile file, ModelMap
model)
{
//Code to save the image
}

这个异常有解决办法吗?我已经根据以下堆栈解决方案进行了尝试,但仍然遇到相同的异常。

Jersey client exception: A message body writer was not found

Sending multiple files with Jersey: MessageBodyWriter not found for multipart/form-data

最佳答案

您是否添加了多部分的依赖项?

<!-- Jersey client support  -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.9</version>
</dependency>

<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.9</version>
</dependency>

<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-multipart</artifactId>
<version>1.9</version>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>

<!-- Apache Commons FileUpload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>

关于java - ClientHandlerException : MIME media type, 多部分/表单数据,未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50754650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com