使用来自的休息Web服务下载文件表单数据库

时间:2016-01-07 10:56:15

标签: java rest

我想使用一个其他网络服务从一个数据库下载图像文件。数据库的名称是Reservation,并且有一个Blob字段。我用了两节课。第一个(DatabaseRESTXML.java)是其余的Web服务,第二个(ImageFile.java)从数据库中获取该文件。我的代码是:

DatabaseRESTXML.java:

import java.io.StringWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.xml.bind.JAXB;

@Path( "Reservation" )
public class DatabaseRESTXML 
{   
    private static final String URL = "jdbc:mysql://localhost/Reservation";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "root";

    private Connection connection = null;
    private PreparedStatement selectImage = null;
    private ResultSet resultSet = null;

    private ImageFile imageFile;

    @GET
    @Path( "getImage" )
    @Produces( "application/xml" )
    public String getImage()
    {
        try 
        {
            Class.forName( "com.mysql.jdbc.Driver" );
            Connection connection = DriverManager.getConnection( URL, USERNAME, PASSWORD );
            selectImage = connection.prepareStatement( "SELECT Image FROM seats" );
            resultSet = selectImage.executeQuery();

            if ( resultSet.next() )
            {
                imageFile.setImageBlob( resultSet.getBlob( 1 ) );
                imageFile.setImageBytes();
                imageFile.setImageStream();
            }   
        } 
        catch ( ClassNotFoundException | SQLException e )
        {
            e.printStackTrace();
        }
        finally
        {
            try 
            {
                resultSet.close();
                selectImage.close();
                connection.close();
            } 
            catch (SQLException e) 
            {
                e.printStackTrace();
            }
        }

        StringWriter writer = new StringWriter();
        JAXB.marshal( imageFile, writer );

        return writer.toString();
    }
}

ImageFIle.java:

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Blob;
import java.sql.SQLException;

public class ImageFile 
{
    private Blob imageBlob = null;
    private FileOutputStream imageStream = null;
    private byte[] imageBytes = null;

    public ImageFile()
    {

    }

    public void setImageBlob( Blob newImageBlob )
    {
        imageBlob = newImageBlob;
    }

    public Blob getImageBlob()
    {
        return imageBlob;
    }

    public void setImageStream()
    {

        try 
        {
            imageStream = new FileOutputStream( "image.jpg" );
            imageStream.write( imageBytes );
            imageStream.close();
        } 
        catch ( FileNotFoundException e )
        {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public FileOutputStream getImageStream()
    {
        return imageStream;
    }

    public void setImageBytes()
    {
        try 
        {
            imageBytes = new byte[ ( int ) imageBlob.length() ];
        } 
        catch ( SQLException e )
        {
            e.printStackTrace();
        }
    }

    public byte[] getImageBytes()
    {
        return imageBytes;
    }   
}

我有以下错误:

java.lang.NullPointerException
    com.rg.databaserestxml.DatabaseRESTXML.getImage(DatabaseRESTXML.java:57)
    sun.reflect.GeneratedMethodAccessor64.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)
    com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
    com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
    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)
    com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
    com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1542)
    com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1473)
    com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419)
    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:558)
    com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:733)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

1 个答案:

答案 0 :(得分:1)

您应该创建对象ImageFile的新实例,或者在设置值之前检查它是否存在。

这一行之后:

private ImageFile imageFile; 

imageFile为空。

在行中,您尝试在null值:

上设置值
if ( resultSet.next() )
  {
    imageFile.setImageBlob( resultSet.getBlob( 1 ) );
    imageFile.setImageBytes();
    imageFile.setImageStream();
  }   

你应该添加:

if ( resultSet.next() )
  {
    imageFile = new ImageFile();
    imageFile.setImageBlob( resultSet.getBlob( 1 ) );
    imageFile.setImageBytes();
    imageFile.setImageStream();
  }