在浏览器中使用泽西显示Blob

时间:2013-12-23 00:50:55

标签: java web-services rest jersey blob

我一直得到image == null!从服务器。我只是试图显示我从fileinputstream上传到mysql的图像作为blob。在数据库中,它显示为bufferedinputstream fyi。我硬编码,看看我是否能得到一个结果。你们能看到我做错了吗?

@Path("/photo" )
public class DisplayPhoto { 

@GET
@Path("{id}")
 @Produces("image/*")
public Response post(@PathParam("id") String id) {
    Connection con = connection();

    BufferedImage image = null;
    try {
        image = ImageIO.read( getPhoto(con).getBinaryStream() );
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



    ByteArrayOutputStream baos2 = new ByteArrayOutputStream();

    try {
        ImageIO.write( image, "jpg", baos2 );
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }




    return Response.ok(baos2.toByteArray()).build();

}



public Connection connection(){
    Connection con = null;

    try {// set up driver for database
        Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }


    try {
        con = DriverManager.getConnection("jdbc:mysql://aa1c9da17owdhky.cotr7twg0ekb.us-west-2.rds.amazonaws.com/test","nightskycode","uocb4t111");
    //  boolean reachable = con.isValid(10);// check for connection to DB


    } catch (SQLException e) {
        e.printStackTrace();
        System.out.println("No go!3");
    }
    return con;
  }

public Blob getPhoto(Connection con){

    Blob photo = null;
    Statement stmt = null;
    ResultSet rs = null;


    try {
        stmt = con.createStatement();
        rs = stmt.executeQuery("Select photo from photos where photo_id = 3");

        if (stmt.execute("Select photo from photos where photo_id = 3")) {
            rs = stmt.getResultSet();

            while (rs.next()) { // results here
           photo =  rs.getBlob("photo");
            System.out.println(rs.getString("photo")); }

        }

    } 
    catch (SQLException ex){
        // handle any errors
        System.out.println("SQLException: " + ex.getMessage());
        System.out.println("SQLState: " + ex.getSQLState());
        System.out.println("VendorError: " + ex.getErrorCode());
    }
    return photo;

}






**here is the trace**

Dec 22, 2013 9:29:35 PM org.apache.catalina.core.StandardWrapperValve invoke

SEVERE:servlet [ServletAdaptor]的Servlet.service()在路径[/ restService]的上下文中引发了异常 java.lang.IllegalArgumentException:image == null!     at javax.imageio.ImageTypeSpecifier.createFromRenderedImage(Unknown Source)     在javax.imageio.ImageIO.getWriter(未知来源)     在javax.imageio.ImageIO.write(未知来源)     at restService.status.DisplayPhoto.post(DisplayPhoto.java:63)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)     at java.lang.reflect.Method.invoke(Unknown Source)     at com.sun.jersey.spi.container.JavaMethodInvokerFactory $ 1.invoke(JavaMethodInvokerFactory.java:60)     at com.sun.jersey.server.impl.model.method.dispatch.Abs​​tractResourceMethodDispatchProvider $ ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205)     at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)     在com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)     在com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)     在com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)     在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)     在com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409)     在com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:540)     在com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:715)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:722)     在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)     在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)     在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)     在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)     在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)     在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)     在org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)     在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)     在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)     在org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)     在org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:188)     at org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:302)     at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)     at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source)     在java.lang.Thread.run(未知来源)

0 个答案:

没有答案