死亡历险,DELPHI以string方式传递图片到JAVA的WEBSERVICE保存
2016-04-22 14:29
579 查看
死亡历险,DELPHI以string方式传递图片到JAVA的WEBSERVICE保存
procedure TForm1.btn7Click(Sender: TObject);
var
addResult: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;
}
procedure TForm1.btn7Click(Sender: TObject);
var
addResult: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以TByteDynArray(Bytes)方式传递图片到JAVA的WEBSERVICE保存
- RAD Studio, Delphi and C++Builder 10.1 Berlin 新版本功能介绍 - 首场在线教学
- Delphi快捷键
- delphi学习_安装
- delphi TClientDataSet 保存到XML
- delphi 编码速度提升技能
- [Delphi] Search path vs Library Path vs Browsing Path
- delphi 接口Interface
- DELPHI MAKEWORD的用法
- delphi absolute 应用实例
- Delphi常见错误及解决方法
- Delphi 调用C#编写的WebService 参数为Null解决方法
- DELPHI中的消息处理机制
- Delphi 中 函数参数中的 const 修饰符的本质以及注意事项
- DELPHI用const来提高应用程序在多核多线程下的性能
- delphi编写分页显示的模板
- Delphi 关闭MDI子窗口
- DELPHI 参数前缀的使用
- Delphi - 字符串 详解
- 分析函数调用堆栈的原理和Delphi实现