Java Jersey注册Multipart表单上载文件服务 - 错误:未找到消息正文阅读器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java Jersey注册Multipart表单上载文件服务 - 错误:未找到消息正文阅读器相关的知识,希望对你有一定的参考价值。

我是RESTful服务和Jersey的新手。我正在尝试使用enctype mutipart / form-data创建一个响应POST调用的ws。

首先,我有我的应用程序类

import com.ibm.sampleapp.rest.ConversationWS;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.MultiPartFeature;

@ApplicationPath("/api")
public class WinkApplication extends Application {

@Override
public Set<Class<?>> getClasses() {
enter code here
    // Returns the list of classes which are to be added as REST endpoints
    Set<Class<?>> classes = new HashSet<Class<?>>();
    classes.add(MultiPartFeature.class);
    classes.add(FormDataContentDisposition.class);
    classes.add(RESTws.class);
    return classes;
}

然后我有我的休息服务课

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.NumberFormat;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.OPTIONS;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.FeatureContext;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.glassfish.jersey.server.ResourceConfig;

@Path("")
public class RESTws extends ResourceConfig {

@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
public Response uploadFile(
        @FormDataParam("file") InputStream uploadedInputStream,
        @FormDataParam("file") FormDataContentDisposition fileMetaData) throws Exception {

    final ResourceConfig resourceConfig = new ResourceConfig();
    resourceConfig.register(MultiPartFeature.class);
    resourceConfig.register(FormDataContentDisposition.class);

    String filename = fileMetaData.getFileName();


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

    String output = "File uploaded to: " + uploadedFileLocation;

    System.out.println("File uploaded..");
    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();
        } catch (IOException e) {

            e.printStackTrace();
        }

    }

}

最后我有index.html,我上传文件

<!DOCTYPE html>
<html>
<head>
<title>Java Web Starter Application</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="style.css" />
</head>
<body>


<h1>RESTful Web Service - File Upload Example</h1>  
<form action="http://localhost:9080/java-sample-backend/api/upload" method="post" enctype="multipart/form-data">  
<p>  
    Select a file to Upload to server: <input type="file" name="file" size="60" />  
</p>  
<input type="submit" value="Upload File" />  
</form>  


</body>
</html>

我的依赖是:

 <!-- Jersey -->       
 <dependency>
     <groupId>org.glassfish.jersey.containers</groupId>
     <artifactId>jersey-container-servlet</artifactId>
     <version>2.22.2</version>
 </dependency>
 <dependency>
     <groupId>org.glassfish.jersey.media</groupId>
     <artifactId>jersey-media-json-jackson</artifactId>
     <version>2.22.2</version>
 </dependency>
 <dependency>
     <groupId>org.glassfish.jersey.media</groupId>
     <artifactId>jersey-media-sse</artifactId>
     <version>2.22.2</version>
 </dependency>
 <dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-multipart</artifactId>
    <version>2.2</version> 
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.bundles</groupId>
    <artifactId>jaxrs-ri</artifactId>
    <version>2.21</version>
</dependency>

当我尝试通过我的html上传文件时,我收到以下错误:

[错误]未找到org.glassfish.jersey.media.multipart.FormDataContentDisposition类的消息正文阅读器,ContentType:multipart / form-data; boundary = ---------------- ----------- 7e21f97403c2

谷歌搜索问题我明白我的问题是我没有注册我的服务,但我无法理解我是如何实现它的。

有没有办法以编程方式而不是通过web.xml文件? - 非常感谢您的帮助。

答案

你需要一个像MyRestApplication extends ResourceConfig这样的类,用@ApplicationPath("/PATH")注释。在这个类的构造函数中,您调用register( MultiPartFeature.class );并使用registerClasses( MyService.class );注册您的服务类。

MyService不扩展任何其他类,但必须由@Path("/SERVICEPATH")注释。然后该类包含方法uploadFile

你不需要web.xml

以上是关于Java Jersey注册Multipart表单上载文件服务 - 错误:未找到消息正文阅读器的主要内容,如果未能解决你的问题,请参考以下文章

Jersey 服务文件上传导致 OutOfMemoryError

Jersey API中的多部分/表单数据出现CORS错误

Jersey API 中的 multipart/form-data 出现 CORS 错误

Spring Boot + Jersey 类型过滤器 - 服务的错​​误请求 400 消耗 MULTIPART_FORM_DATA

Java Rest Jersey:发布多种类型的数据(文件和JSON)

如何将PNG图像从Jersey REST服务方法返回到浏览器