Panagiotis
Panagiotis

Reputation: 511

Download file form database using rest web service from

I want to download an image file from one database using one rest web service. The name of the database is Reservation and has one Blob field. I use two classes. The first (DatabaseRESTXML.java) is the rest web service and the second (ImageFile.java) takes the file from the database. My code is:

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;
    }   
}

I have the following error:

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)

Upvotes: 0

Views: 657

Answers (1)

Mateusz Sroka
Mateusz Sroka

Reputation: 2335

You should create new instance of object ImageFile or check if it exists before setting values in it.

After this line:

private ImageFile imageFile; 

imageFile is null.

In lines you are trying to set values on null value:

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

you should add:

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

Upvotes: 1

Related Questions