在java中通过jersey上传文件时出错

时间:2017-10-05 06:19:56

标签: java xml rest jersey

我是泽西岛的新手。

我想将excel / any文件上传到服务器。 我已经尝试了

这是我的pom.xml文件

<build>
    <finalName>eduvib</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.5.1</version>

        </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>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>

    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
    </dependency>

</dependencies>
<properties>
    <jersey.version>2.16</jersey.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<packaging>war</packaging>

这是我在球衣上的休息服务

@POST
@Path("/BulkQuestion")
@Consumes({MediaType.MULTIPART_FORM_DATA})
public Response uploadPdfFile(  @FormDataParam("file") InputStream fileInputStream,
                                @FormDataParam("file") FormDataContentDisposition fileMetaData) throws Exception
{
    String UPLOAD_PATH = "/home/pavan/Desktop/";
    try
    {
        int read = 0;
        byte[] bytes = new byte[1024];

        OutputStream out = new FileOutputStream(new File(UPLOAD_PATH + fileMetaData.getFileName()));
        while ((read = fileInputStream.read(bytes)) != -1)
        {
            out.write(bytes, 0, read);
        }
        out.flush();
        out.close();
    } catch (IOException e)
    {
        throw new WebApplicationException("Error while uploading file. Please try again !!");
    }
    return Response.ok("Data uploaded successfully !!").build();
}

但是,当我把这个休息服务我的应用程序不会运行(没有Api工作)它给500状态代码服务器错误 这是我得到的错误日志:

&#13;
&#13;
    org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization.
[[FATAL] No injection source found for a parameter of type public javax.ws.rs.core.Response com.gary.server.eduvib.resource.QuestionResource.uploadPdfFile(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition) throws java.lang.Exception at index 0.; source='ResourceMethod{httpMethod=POST, consumedTypes=[multipart/form-data], producedTypes=[], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class com.gary.server.eduvib.resource.QuestionResource, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@48301a35]}, definitionMethod=public javax.ws.rs.core.Response com.gary.server.eduvib.resource.QuestionResource.uploadPdfFile(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition) throws java.lang.Exception, parameters=[Parameter [type=class java.io.InputStream, source=file, defaultValue=null], Parameter [type=class org.glassfish.jersey.media.multipart.FormDataContentDisposition, source=file, defaultValue=null]], responseType=class javax.ws.rs.core.Response}, nameBindings=[]}']
	org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:502)
	org.glassfish.jersey.server.ApplicationHandler.access$500(ApplicationHandler.java:166)
&#13;
&#13;
&#13;

0 个答案:

没有答案