简体   繁体   English

使用Rest Web Service从以下位置下载文件表单数据库

[英]Download file form database using rest web service from

I want to download an image file from one database using one rest web service. 我想使用一个Rest Web服务从一个数据库下载一个图像文件。 The name of the database is Reservation and has one Blob field. 该数据库的名称为Reservation,并且具有一个Blob字段。 I use two classes. 我使用两个类。 The first (DatabaseRESTXML.java) is the rest web service and the second (ImageFile.java) takes the file from the database. 第一个(DatabaseRESTXML.java)是其余的Web服务,第二个(ImageFile.java)从数据库中获取文件。 My code is: 我的代码是:

DatabaseRESTXML.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 : 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)

You should create new instance of object ImageFile or check if it exists before setting values in it. 您应该创建对象ImageFile新实例,或者在设置值之前检查它是否存在。

After this line: 在此行之后:

private ImageFile imageFile; 

imageFile is null. imageFile为null。

In lines you are trying to set values on null value: 在各行中,您尝试将值设置为null值:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM