java - Unsupported Media Type error in Jersey API -
i have seen multiple answers on stackoverflow , yet unable identify wrong pom.xml. have upload files on server, using modified code formdatamultipart of this link
i have 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 file server savefile(fileinputstream, filepath); string output = "file saved server location using formdatamultipart : " + filepath; return response.status(200).entity(output).build(); } // save uploaded file defined location on 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 as
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/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 following artifactid if don't need servlet 2.x compatibility --> <!-- artifactid>jersey-container-servlet</artifactid --> </dependency> <!-- uncomment json support--> <dependency> <groupid>org.glassfish.jersey.media</groupid> <artifactid>jersey-media-moxy</artifactid> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version>5.1.18</version> </dependency> <!-- https://mvnrepository.com/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 using jersey version 2.16, still getting "unsupported media type", though using jersey-media-multipart version 2.25. there issue due version? or wrong pom.xml??
thanks, grateful help.
you still need register multipartfeature
comes jersey-media-multipart
. feature register providers needed handle multipart requests. error you're seeing because there no providers registered can handle formdatamultipart
. see this post couple different ways of registering it
Comments
Post a Comment