gpt4 book ai didi

java - 状态码 415 : No MessageBodyReader Found for multipart/form-data, FormDataMultiPart

转载 作者:行者123 更新时间:2023-11-28 21:53:30 27 4
gpt4 key购买 nike

我在将 multipart/form-data 发布到我创建的 RESTful Web 服务时遇到了一些问题。我正在尝试通过 RESTful Web 服务上传媒体文件(图像、视频、音频)。我四处搜索以找到执行此操作的最佳方法,并发现 POSTing multipart/form-data 是最佳解决方案。

问题是当我发布一些多部分/表单数据时,我在我的 Tomcat 服务器中收到此错误消息:

SEVERE MessageBodyReader not found for media type=multipart/form-data; boundary=----WebKitFormBoundaryTg7uVLcYJ3lsBpQE, type=class org.glassfish.jersey.media.multipart.FormDataMultiPart, genericType=class org.glassfish.jersey.media.multipart.FormDataMultiPart.

我确实尝试在 stackoverflow 上寻找答案,但问题似乎是很多人都缺少 mimepull.jar。我检查以确保 mimepull.jar 在我的类路径中并且确实在,所以这不是问题。在这一点上我被卡住了。

这是我的 pom.xml 中的依赖项:

<dependencies>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.12</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.12</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.12</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.12</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.4.2</version>
</dependency>
</dependencies>

这是处理多部分/表单数据 POST 的相关后端代码:

@POST
@Path("media")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public Response uploadFile(FormDataMultiPart form ) {

FormDataBodyPart filePart = form.getField("file");
ContentDisposition headerofFilePart = filePart.getContentDisposition();
InputStream uploadedInputStream = filePart.getValueAs(InputStream.class);

String uploadedFileLocation = "C:\\surveymedia\\media" + headerofFilePart.getFileName();

try {
saveFile(uploadedInputStream, uploadedFileLocation);
} catch (Exception e) {
return Response.status(400).entity(e.getCause()).build();
}

String output = "File uploaded to: " + uploadedFileLocation;
return Response.status(200).entity(output).build();
}

最后是我用来测试发送文件到后端的测试页面:

<html>
<head>
<title></title>
</head>
<body>
<h1>File Upload with Jersey</h1>

<form action="/rest/surveys/media" method="post" enctype="multipart/form-data">

<p>
Select a file : <input type="file" name="file" size="45" />
</p>

<input type="submit" value="Upload It" />
</form>
</body>
</html>

如果您需要更多信息,请告诉我。在此先感谢您的帮助!

最佳答案

我在通过 Jersey 实现文件 uploader 时遇到了这个类似的问题,所以我最终采用了一种稍微不同的方法,方法的参数是 InputStream 和 FormDataContentDisposition 对象。

这是一个例子;也许这对你有用:

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces({MediaType.APPLICATION_JSON})
public Response uploadFile(
@FormDataParam("file") InputStream uploadedInputStream,
@FormDataParam("file") FormDataContentDisposition fileDetail) throws Exception {

String filename = fileDetail.getFileName();
String uploadedFileLocation = "C:\\surveymedia\\media" + filename;
try {
saveFile(uploadedInputStream, uploadedFileLocation);
}
catch(Exception e){
return Response.status(400).entity(e.getCause()).build();
}

String output = "File uploaded to: " + uploadedFileLocation;
return Response.status(200).entity(output).build();
}

关于java - 状态码 415 : No MessageBodyReader Found for multipart/form-data, FormDataMultiPart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26590431/

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