Jersey API中不支持的媒体类型错误

编程入门 行业动态 更新时间:2024-10-28 04:24:50
本文介绍了Jersey API中不支持的媒体类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在stackoverflow上看到了多个答案,但无法识别pom.xml出了什么问题. 我必须在服务器上上传文件,所以我使用的是此链接

我的代码为:

import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.ws.rs.Consumes; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import org.glassfish.jersey.media.multipart.ContentDisposition; import org.glassfish.jersey.media.multipart.FormDataBodyPart; import org.glassfish.jersey.media.multipart.FormDataMultiPart; @Path("/files") public class JerseyFileUpload { private static final String SERVER_UPLOAD_LOCATION_FOLDER = "E:/Upload_Files/"; @POST @Path("/upload") @Consumes(MediaType.MULTIPART_FORM_DATA) public Response uploadFile(FormDataMultiPart form) { FormDataBodyPart filePart = form.getField("file"); ContentDisposition headerOfFilePart = filePart.getContentDisposition(); InputStream fileInputStream = filePart.getValueAs(InputStream.class); String filePath = SERVER_UPLOAD_LOCATION_FOLDER + headerOfFilePart.getFileName(); // save the file to the server saveFile(fileInputStream, filePath); String output = "File saved to server location using FormDataMultiPart : " + filePath; return Response.status(200).entity(output).build(); } // save uploaded file to a defined location on the server private void saveFile(InputStream uploadedInputStream, String serverLocation) { try { OutputStream outpuStream = new FileOutputStream(new File( serverLocation)); int read = 0; byte[] bytes = new byte[1024]; outpuStream = new FileOutputStream(new File(serverLocation)); while ((read = uploadedInputStream.read(bytes)) != -1) { outpuStream.write(bytes, 0, read); } outpuStream.flush(); outpuStream.close(); uploadedInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }

我的pom.xlm是

<project xmlns="maven.apache/POM/4.0.0" xmlns:xsi="www.w3/2001/XMLSchema-instance" xsi:schemaLocation="maven.apache/POM/4.0.0 maven.apache/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>Photographer</groupId> <artifactId>Assignment</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>Assignment</name> <build> <finalName>Assignment</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.5.1</version> <inherited>true</inherited> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </build> <dependencyManagement> <dependencies> <dependency> <groupId>org.glassfish.jersey</groupId> <artifactId>jersey-bom</artifactId> <version>${jersey.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet-core</artifactId> <!-- use the following artifactId if you don't need servlet 2.x compatibility --> <!-- artifactId>jersey-container-servlet</artifactId --> </dependency> <!-- uncomment this to get JSON support--> <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-moxy</artifactId> </dependency> <!-- mvnrepository/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.18</version> </dependency> <!-- mvnrepository/artifact/org.glassfish.jersey.media/jersey-media-multipart --> <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-multipart</artifactId> <version>2.25</version> </dependency> <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-server</artifactId> </dependency> </dependencies> <properties> <jersey.version>2.16</jersey.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties>

我使用的是Jersey版本2.16,即使我使用的是jersey-media-multipart版本2.25,我仍然收到不受支持的媒体类型".版本是否存在问题?还是这个pom.xml有什么问题?

感谢您的帮助.

解决方案

您仍然需要注册jersey-media-multipart随附的MultiPartFeature.该功能将注册处理多部分请求所需的提供程序.您当前看到的错误是因为没有注册可以处理FormDataMultiPart的提供程序.有关几种不同的注册方式,请参见这篇文章.

I have seen multiple answers on stackoverflow and yet unable to identify what is wrong with my pom.xml. I have to upload files on the server, so I am using the modified code from FormDataMultiPart of this link

I have the code as:

import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.ws.rs.Consumes; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import org.glassfish.jersey.media.multipart.ContentDisposition; import org.glassfish.jersey.media.multipart.FormDataBodyPart; import org.glassfish.jersey.media.multipart.FormDataMultiPart; @Path("/files") public class JerseyFileUpload { private static final String SERVER_UPLOAD_LOCATION_FOLDER = "E:/Upload_Files/"; @POST @Path("/upload") @Consumes(MediaType.MULTIPART_FORM_DATA) public Response uploadFile(FormDataMultiPart form) { FormDataBodyPart filePart = form.getField("file"); ContentDisposition headerOfFilePart = filePart.getContentDisposition(); InputStream fileInputStream = filePart.getValueAs(InputStream.class); String filePath = SERVER_UPLOAD_LOCATION_FOLDER + headerOfFilePart.getFileName(); // save the file to the server saveFile(fileInputStream, filePath); String output = "File saved to server location using FormDataMultiPart : " + filePath; return Response.status(200).entity(output).build(); } // save uploaded file to a defined location on the server private void saveFile(InputStream uploadedInputStream, String serverLocation) { try { OutputStream outpuStream = new FileOutputStream(new File( serverLocation)); int read = 0; byte[] bytes = new byte[1024]; outpuStream = new FileOutputStream(new File(serverLocation)); while ((read = uploadedInputStream.read(bytes)) != -1) { outpuStream.write(bytes, 0, read); } outpuStream.flush(); outpuStream.close(); uploadedInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }

My pom.xlm is as

<project xmlns="maven.apache/POM/4.0.0" xmlns:xsi="www.w3/2001/XMLSchema-instance" xsi:schemaLocation="maven.apache/POM/4.0.0 maven.apache/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>Photographer</groupId> <artifactId>Assignment</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>Assignment</name> <build> <finalName>Assignment</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.5.1</version> <inherited>true</inherited> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </build> <dependencyManagement> <dependencies> <dependency> <groupId>org.glassfish.jersey</groupId> <artifactId>jersey-bom</artifactId> <version>${jersey.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet-core</artifactId> <!-- use the following artifactId if you don't need servlet 2.x compatibility --> <!-- artifactId>jersey-container-servlet</artifactId --> </dependency> <!-- uncomment this to get JSON support--> <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-moxy</artifactId> </dependency> <!-- mvnrepository/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.18</version> </dependency> <!-- mvnrepository/artifact/org.glassfish.jersey.media/jersey-media-multipart --> <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-multipart</artifactId> <version>2.25</version> </dependency> <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-server</artifactId> </dependency> </dependencies> <properties> <jersey.version>2.16</jersey.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties>

I am using Jersey Version 2.16, I am still getting "Unsupported Media Type", even though I am using the jersey-media-multipart version 2.25. Is there some issue due to the version? Or what is wrong with this pom.xml??

Thanks, would be grateful for any help.

解决方案

You still need to register the MultiPartFeature that comes with the jersey-media-multipart. The feature will register the providers needed to handle multipart requests. The error you're currently seeing is because there are no providers registered that can handle the FormDataMultiPart. See this post for a couple different ways of registering it

更多推荐

Jersey API中不支持的媒体类型错误

本文发布于:2023-11-16 21:21:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1607507.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中不   错误   类型   媒体   Jersey

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!