Java Webservice 取数据库图片,图片以数组方式传递到Delphi前台显示

时间:2022-01-01 21:59:41
login.java

public String[] lookImage_Array(Connection conn, String sID) throws SQLException {
        String sql = "";
        String[] result = new String[3];
        String[] lookImage_Array = new String[3];
        String result1 = "";
        String result2 = "";
        String result3 = "";
        byte[] blob1 = null;
        byte[] blob2 = null;
        byte[] blob3 = null;


    sql = "select image1,image2,image3 from D_J_TABLE1 where id='"+sID+"' ";
   
        pstmt = conn.prepareStatement(sql); //根据sql创建PreparedStatement
        rs = pstmt.executeQuery(); //执行查询,返回结果集
        if (rs.next()) {
        if(rs.getBytes(1)!=null)
        {
        blob1 = rs.getBytes(1);
        result1 = new sun.misc.BASE64Encoder().encode(blob1);
        lookImage_Array[0] = result1;
        //System.out.println(result1);
        }
       
        if(rs.getBytes(2)!=null)
        {
        blob2 = rs.getBytes(2);
            result2 = new sun.misc.BASE64Encoder().encode(blob2);
            lookImage_Array[1] = result2;
       
       
        if(rs.getBytes(3)!=null)
        {
        blob3 = rs.getBytes(3);
            result3 = new sun.misc.BASE64Encoder().encode(blob3);
            lookImage_Array[2] = result3;
        }
       
        result = lookImage_Array;
        System.out.println(result);
       
        if(rs.getBytes(1)==null && rs.getBytes(2)==null && rs.getBytes(3)==null)
        {
        lookImage_Array[0] = "repeat";
        result = lookImage_Array;
        }
        } 
        else
        {
        lookImage_Array[0] = "repeat";
        result = lookImage_Array;
        }
        return result;
    }


ServiceHello.java


public String[] lookImage_Array(String sID) {
String[] result = new String[3];
try {
login getUserList00=new login();
result = getUserList00.lookImage_Array(conn, sID);
} catch (Exception e) {
            e.printStackTrace();
        }
return result;
}


Unit1.pas




procedure TForm1.btn19Click(Sender: TObject);
var
  addResult,result1,result2,result3,result0:string;


  ss,ss2,ss3:TStringStream;
  ms,ms2,ms3:TMemoryStream;
  bitmap:TBitmap;
  JpgFile:TjpegImage;
  gif:TGIFImage;


  Buffer1,Buffer2,Buffer3: Word;
  png: TPNGObject;


  lookImageString : Array_Of_string;
begin
  SetLength(lookImageString,3);
  
  lookImageString:=ServiceHello1.lookImage_Array(dbgrd1.Columns.Items[1].Field.Value);


  result1:=lookImageString[0];
  result2:=lookImageString[1];
  result3:=lookImageString[2];


  if (result1='repeat') then
  begin
    ShowMessage('无图片');
  end
  else
  begin
    try
      ss := TStringStream.Create(result1);
      ms := TMemoryStream.Create;
      DecodeStream(ss,ms);//将base64字符流还原为内存流
      ms.Position:=0;//移动指针到最开头的位置
      if ms.Size = 0 then   //如果文件大小等于0,那么
      begin
        //ShowMessage('无图片1');
      end
      else
      begin
        ms.ReadBuffer(Buffer1, 2);
        ms.Position:=0;


        if Buffer1 = $4D42 then //bmp
        begin
          bitmap := TBitmap.Create;
          bitmap.LoadFromStream(ms);
          img1.Picture.Bitmap.Assign(bitmap);
          bitmap.Free;
        end
        else if Buffer1 = $D8FF then //jpg
        begin
          JpgFile := TjpegImage.Create;
          JpgFile.LoadFromStream(ms);
          img1.Picture.Assign(JpgFile);
          JpgFile.Free;
        end
        else if Buffer1 = $5089 then//PNG
        begin
          png := TPNGObject.Create;
          png.LoadFromStream(ms);
          img1.Picture.Bitmap.Assign(png);
          png.Free;
        end
        else if Buffer1 = $4947 then //gif
        begin
          gif := TGifImage.Create;
          gif.LoadFromStream(ms);
          img1.Picture.Assign(gif);
          gif.Free;
        end;
      end;


      ss2 := TStringStream.Create(result2);
      ms2 := TMemoryStream.Create;
      DecodeStream(ss2,ms2);//将base64字符流还原为内存流
      ms2.Position:=0;//移动指针到最开头的位置
      if ms2.Size = 0 then   //如果文件大小等于0,那么
      begin
        //ShowMessage('无图片2');
      end
      else
      begin
        ms2.ReadBuffer(Buffer2, 2);
        ms2.Position:=0;


        if Buffer2 = $4D42 then //bmp
        begin
          bitmap := TBitmap.Create;
          bitmap.LoadFromStream(ms2);
          img2.Picture.Bitmap.Assign(bitmap);
          bitmap.Free;
        end
        else if Buffer2 = $D8FF then //jpg
        begin
          JpgFile := TjpegImage.Create;
          JpgFile.LoadFromStream(ms2);
          img2.Picture.Assign(JpgFile);
          JpgFile.Free;
        end
        else if Buffer2 = $5089 then//PNG
        begin
          png := TPNGObject.Create;
          png.LoadFromStream(ms2);
          img2.Picture.Bitmap.Assign(png);
          png.Free;
        end
        else if Buffer2 = $4947 then //gif
        begin
          gif := TGifImage.Create;
          gif.LoadFromStream(ms2);
          img2.Picture.Assign(gif);
          gif.Free;
        end;
      end;


      ss3 := TStringStream.Create(result3);
      ms3 := TMemoryStream.Create;
      DecodeStream(ss3,ms3);//将base64字符流还原为内存流
      ms3.Position:=0;//移动指针到最开头的位置
      if ms3.Size = 0 then   //如果文件大小等于0,那么
      begin
        //ShowMessage('无图片2');
      end
      else
      begin
        ms3.ReadBuffer(Buffer3, 2);
        ms3.Position:=0;


        if Buffer3 = $4D42 then //bmp
        begin
          bitmap := TBitmap.Create;
          bitmap.LoadFromStream(ms3);
          img3.Picture.Bitmap.Assign(bitmap);
          bitmap.Free;
        end
        else if Buffer3 = $D8FF then //jpg
        begin
          JpgFile := TjpegImage.Create;
          JpgFile.LoadFromStream(ms3);
          img3.Picture.Assign(JpgFile);
          JpgFile.Free;
        end
        else if Buffer3 = $5089 then//PNG
        begin
          png := TPNGObject.Create;
          png.LoadFromStream(ms3);
          img3.Picture.Bitmap.Assign(png);
          png.Free;
        end
        else if Buffer3 = $4947 then //gif
        begin
          gif := TGifImage.Create;
          gif.LoadFromStream(ms3);
          img3.Picture.Assign(gif);
          gif.Free;
        end;
      end;


    finally
      ss.Free;
      ms.Free;


      ss2.Free;
      ms2.Free;
    end;
  end;
end;