从InputStream转换为Blob时出错

时间:2015-02-22 15:16:16

标签: java rest file-upload jax-rs blob

我一直在工作JAX-RS Jersey。我试图将数据库中的文件存储为Blob(DB中的LongBlob)。这是我到目前为止实施的代码。

在Html中

<form id="documents" method="post" action="api/upload/documents" enctype="multipart/form-data">
    <div class="col-md-12">
        <label class="label">Attach File</label> <input type="file" id="file" name="file"/>
    </div>
    <div class="col-md-12">
        <input type="submit" value="Submit" />
    </div>
</form>

在Java中

@WebService
@Path("/upload")
public class UploadService {

    @POST
    @Path("/documents")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response loanDocuments(@FormDataParam("file") InputStream uploadedInputStream,@FormDataParam("file") FormDataContentDisposition fileDetail) {
        Upload upload = uploadDao.getDetailsById(2); // It'll fetch the existing object from db
        upload.setUploadFiles((Blob) uploadedInputStream);
        uploadDao.updateUpload(upload);
        return Response.status(200).entity("FILE UPLOAD SUCCESS").build();
    }
}

上传模型

@Entity
@Table(name = "files")
public class Loan {
    @Id 
    @GeneratedValue
    @Column(name = "id", length=10)
    private int id;

    @Column(name = "file")
    private Blob payslips;

    // Getter and setters
}

我能够在java中成功接收上传的文件,但在将其转换为Blob时会出现问题。

这是Stacktrace

java.lang.ClassCastException: org.jvnet.mimepull.DataHead$ReadMultiStream cannot be cast to java.sql.Blob
    at LOCATION OF MY ERROR - Exactly at the uploadedInputStream to Blob statement)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
    at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205)
    at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
    at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1542)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1473)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409)
    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:558)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:733)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.opentap.filter.SessionFilter.doFilter(SessionFilter.java:57)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:526)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:655)
    at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:277)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2381)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2370)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

我尝试在Modal中使用BinaryStream而不是Blob,这让我在tomcat启动时出现内存泄漏。我搜索了InputStream以进行blob转换,但我无法找到任何有助于我的情况的内容。

1 个答案:

答案 0 :(得分:0)

我确信InputStreamBlob转换不应该太难。但更简单的方法是使用byte[]。然后你可以实例化一个SerialBlob

@POST
@Path("/documents")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response loanDocuments(@FormDataParam("file") byte[] uploadedByteArray,
                              @FormDataParam("file") FormDataContentDisposition detail) 
                              throws SQLException {

    java.sql.Blob blob = new javax.sql.rowset.serial.SerialBlob(uploadedByteArray);

    return Response.status(200).entity("FILE UPLOAD SUCCESS").build();
}