死亡历险,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;
}