死亡历险,DELPHI以string方式传递图片到JAVA的WEBSERVICE保存
procedure TForm1.btn7Click(Sender: TObject);
varaddResult:string;
strm : TMemoryStream;
i,j:Integer;
imageStr:string;
ms:TMemoryStream;
ss:TStringStream;
s,s2,s3:string;
imageArray : array[0..2] of string;
img:TImage;
begin
if img1.Picture.Graphic<>nil then
begin
strm := TMemoryStream.Create;
img1.Picture.Graphic.SaveToStream(strm);
ss := TStringStream.Create('');
strm.Position:=0;
EncodeStream(strm,ss);//将内存流编码为base64字符流
s:=ss.DataString;
strm.Free;
ss.Free;
end;
if img2.Picture.Graphic<>nil then
begin
strm := TMemoryStream.Create;
img2.Picture.Graphic.SaveToStream(strm);
ss := TStringStream.Create('');
strm.Position:=0;
EncodeStream(strm,ss);//将内存流编码为base64字符流
s2:=ss.DataString;
strm.Free;
ss.Free;
end;
addResult:=ServiceHello1.addUser(edt4.Text, s, s2);
if (addResult='ok') then
begin
ShowMessage('添加成功');
end;
end;
这是JAVA的WEBSERVICE接口
function addUser(const arg0: WideString; const arg1: WideString; const arg2: WideString): WideString; stdcall;
这是JAVA的WEBSERVICE函数
public String addUser(String sname, String imageStr, String imageStr2) {
String result = "";
try {
login getUserList00=new login();
result = getUserList00.addUser(conn, sname, imageStr, imageStr2);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
login.java
public String addUser(Connection conn, String sname, String imageStr, String imageStr2) throws SQLException, IOException {
String sql = "";
String result = "";
byte[] blob1 = null;
sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
blob1 = decoder.decodeBuffer(imageStr);
//System.out.println(blob1);
byte[] blob2 = null;
sun.misc.BASE64Decoder decoder2 = new sun.misc.BASE64Decoder();
blob2 = decoder2.decodeBuffer(imageStr2);
sql = "select * from D_J_TABLE1 where name='"+sname+"' ";//定义SQL语句
pstmt = conn.prepareStatement(sql); //根据sql创建PreparedStatement
rs = pstmt.executeQuery(); //执行查询,返回结果集
if (rs.next()) {
result = "repeat";
}
else
{
Date time = new Date(System.currentTimeMillis());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String current = sdf.format(time);
if("".equals(sname) || sname == null)
{
result = "empty";
}
else
{
sql = "insert into D_J_TABLE1(name,addtime,image1,image2)values('"+sname+"',sysdate,?,?) ";
pstmt = conn.prepareStatement(sql); //根据sql创建PreparedStatement
pstmt.setBytes(1, blob1);
pstmt.setBytes(2, blob2);
rs = pstmt.executeQuery(); //执行查询,返回结果集
if (rs.next()) {
result = "ok";
}
}
}
return result;
}

本文介绍如何使用Delphi将图片转换为Base64字符串,并通过WebService传递给Java后端进行处理和存储的过程。涉及Delphi内存流操作、Base64编码及Java后端解码和数据库插入等关键技术。

3656

被折叠的 条评论
为什么被折叠?



