以下为JAVA代码
public void uploadBusinessImg3() throws Exception {
String host = "http://183.134.253.23:8086/nbyd-parkpot-service-test/";
String url = host + "parkpot/uploadBusinessImg";
HttpPost httpPost = new HttpPost(url); MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
File file = new File("F:/a.jpg");
FileBody fileBody = new FileBody(file);
entityBuilder.addPart("file", fileBody); JSONObject dataObject = new JSONObject();
dataObject.put("portname","parkpottest");
String pwd="tc123456";
dataObject.put("portpwd", DigestUtils.md5Hex(pwd));
dataObject.put("parkpotid", "330000000001");
dataObject.put("businessid", "228");
dataObject.put("businesstype", "1");//业务类型(1:到达 2:离开)
dataObject.put("takeTime", DateUtil.getDate(new Date(),12));//拍照时间
dataObject.put("imgIndex", 2);//照片索引
dataObject.put("imageHash", "055ec8012312ad5b54d94b3e91f5633e");
FileInputStream inputStream = new FileInputStream(file);
dataObject.put("imageHash", DigestUtils.md5Hex(inputStream));
inputStream.close(); StringBody stringBody = new StringBody(dataObject.toString(), ContentType.APPLICATION_JSON);
entityBuilder.addPart("data", stringBody); HttpEntity entity = entityBuilder.build();
httpPost.setEntity(entity); CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse httpResponse = httpClient.execute(httpPost); String result = EntityUtils.toString(httpResponse.getEntity());
System.out.println("result: " + result);
}以下为Delphi代码:
Function LB_uploadBusinessImg( //5.4 车辆业务照片
PicFile :String; //文件路径
businessid :String; //停车场业务编号 (业务编号为一次车辆到达离开的业务ID) 是
businesstype :String; //业务类型(1:到达 2:离开) 是
imgIndex :Integer; //照片索引(每个业务,同一业务类型最多只能上传4张照片,即到达2张,离开2张,当上传的imgIndex大于5,系统将不接收该照片) 是
takeTime :String //格式:yyyyMMddHHmmss 是
): Integer;
var
postStream : TIdMultiPartFormDataStream;
IdHttp : TIdHTTP;
ResponseStream : TStringStream; //返回信息
RecCMD ,IMSUrl: String;
MyMD5 : TIdHashMessageDigest5;
IMSCMD ,Sign,FileMD5 : String;
MyFile :TFileStream;
jo :ISuperObject;
IsSucc : Integer;begin
IF RunParam.LBSC=0 THEN Exit;
if Copy(RunParam.LBDZ,Length(RunParam.LBDZ)-1,1)<>'/' then
IMSUrl :=RunParam.LBDZ+'/parkpot/uploadBusinessImg'
else
IMSUrl :=RunParam.LBDZ+'parkpot/uploadBusinessImg';
MyMD5 :=TIdHashMessageDigest5.Create;
MyFile :=TFileStream.Create(PicFile,fmOpenRead) ;
TRY
//计算文件MD5
try
FileMD5 :=MyMD5.AsHex(MyMD5.HashValue(MyFile)) ;
except
Result :=-3;
SaveFile_LB('---错误: MD5照片错误!----');
Exit;
end;
finally
MyFile.Free;
MyMD5.Free;
end; IdHttp := TIdHTTP.Create(nil); //创建IDHTTP控件
postStream := TIdMultiPartFormDataStream.Create;
ResponseStream := TStringStream.Create('');
try
IdHttp.ConnectTimeout:=3000;
IdHttp.ReadTimeout :=8000;
IdHttp.Request.ContentType := 'multipart/form-data';
postStream.AddFormField('portname' ,RunParam.LB_USE );
postStream.AddFormField('portpwd' ,MDSPassWord(RunParam.LB_PSD) );
postStream.AddFormField('parkpotid' ,RunParam.LB_NO );
postStream.AddFormField('businessid' ,businessid );
postStream.AddFormField('businesstype' ,businesstype );
postStream.AddFormField('imgIndex' , intToStr(imgIndex) );
postStream.AddFormField('takeTime' , takeTime );
postStream.AddFormField('imageHash' , FileMD5 );
//postStream.AddFile ('file' , PicFile, 'image/jpeg'); // 表单文件
try
IdHttp.Post(IMSUrl,postStream,ResponseStream); //获取网页返回的信息
RecCMD := ResponseStream.DataString;
//网页中的存在中文时,需要进行UTF8解码
RecCMD := UTF8Decode(RecCMD);
SaveFile_LB('---照片上传: 上传返回:'+RecCMD);
jo := SO(RecCMD);
IsSucc :=jo['result'].AsInteger;
if IsSucc=1 then //返回错误
Result := -1;
if IsSucc=0 then //成功
Result := 0;
except
on e : Exception do
begin
SaveFile_LB('---照片上传: 上传失败!:'+e.Message);
Result :=-2; //发送数据失败
Exit;
end;
end;
finally
postStream.Free;
ResponseStream.Free;
IdHttp.Free;
end;
end;问题: 照片字段根据JAVA代码 ,不知道Delphi这里如何转换了 ?
public void uploadBusinessImg3() throws Exception {
String host = "http://183.134.253.23:8086/nbyd-parkpot-service-test/";
String url = host + "parkpot/uploadBusinessImg";
HttpPost httpPost = new HttpPost(url); MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
File file = new File("F:/a.jpg");
FileBody fileBody = new FileBody(file);
entityBuilder.addPart("file", fileBody); JSONObject dataObject = new JSONObject();
dataObject.put("portname","parkpottest");
String pwd="tc123456";
dataObject.put("portpwd", DigestUtils.md5Hex(pwd));
dataObject.put("parkpotid", "330000000001");
dataObject.put("businessid", "228");
dataObject.put("businesstype", "1");//业务类型(1:到达 2:离开)
dataObject.put("takeTime", DateUtil.getDate(new Date(),12));//拍照时间
dataObject.put("imgIndex", 2);//照片索引
dataObject.put("imageHash", "055ec8012312ad5b54d94b3e91f5633e");
FileInputStream inputStream = new FileInputStream(file);
dataObject.put("imageHash", DigestUtils.md5Hex(inputStream));
inputStream.close(); StringBody stringBody = new StringBody(dataObject.toString(), ContentType.APPLICATION_JSON);
entityBuilder.addPart("data", stringBody); HttpEntity entity = entityBuilder.build();
httpPost.setEntity(entity); CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse httpResponse = httpClient.execute(httpPost); String result = EntityUtils.toString(httpResponse.getEntity());
System.out.println("result: " + result);
}以下为Delphi代码:
Function LB_uploadBusinessImg( //5.4 车辆业务照片
PicFile :String; //文件路径
businessid :String; //停车场业务编号 (业务编号为一次车辆到达离开的业务ID) 是
businesstype :String; //业务类型(1:到达 2:离开) 是
imgIndex :Integer; //照片索引(每个业务,同一业务类型最多只能上传4张照片,即到达2张,离开2张,当上传的imgIndex大于5,系统将不接收该照片) 是
takeTime :String //格式:yyyyMMddHHmmss 是
): Integer;
var
postStream : TIdMultiPartFormDataStream;
IdHttp : TIdHTTP;
ResponseStream : TStringStream; //返回信息
RecCMD ,IMSUrl: String;
MyMD5 : TIdHashMessageDigest5;
IMSCMD ,Sign,FileMD5 : String;
MyFile :TFileStream;
jo :ISuperObject;
IsSucc : Integer;begin
IF RunParam.LBSC=0 THEN Exit;
if Copy(RunParam.LBDZ,Length(RunParam.LBDZ)-1,1)<>'/' then
IMSUrl :=RunParam.LBDZ+'/parkpot/uploadBusinessImg'
else
IMSUrl :=RunParam.LBDZ+'parkpot/uploadBusinessImg';
MyMD5 :=TIdHashMessageDigest5.Create;
MyFile :=TFileStream.Create(PicFile,fmOpenRead) ;
TRY
//计算文件MD5
try
FileMD5 :=MyMD5.AsHex(MyMD5.HashValue(MyFile)) ;
except
Result :=-3;
SaveFile_LB('---错误: MD5照片错误!----');
Exit;
end;
finally
MyFile.Free;
MyMD5.Free;
end; IdHttp := TIdHTTP.Create(nil); //创建IDHTTP控件
postStream := TIdMultiPartFormDataStream.Create;
ResponseStream := TStringStream.Create('');
try
IdHttp.ConnectTimeout:=3000;
IdHttp.ReadTimeout :=8000;
IdHttp.Request.ContentType := 'multipart/form-data';
postStream.AddFormField('portname' ,RunParam.LB_USE );
postStream.AddFormField('portpwd' ,MDSPassWord(RunParam.LB_PSD) );
postStream.AddFormField('parkpotid' ,RunParam.LB_NO );
postStream.AddFormField('businessid' ,businessid );
postStream.AddFormField('businesstype' ,businesstype );
postStream.AddFormField('imgIndex' , intToStr(imgIndex) );
postStream.AddFormField('takeTime' , takeTime );
postStream.AddFormField('imageHash' , FileMD5 );
//postStream.AddFile ('file' , PicFile, 'image/jpeg'); // 表单文件
try
IdHttp.Post(IMSUrl,postStream,ResponseStream); //获取网页返回的信息
RecCMD := ResponseStream.DataString;
//网页中的存在中文时,需要进行UTF8解码
RecCMD := UTF8Decode(RecCMD);
SaveFile_LB('---照片上传: 上传返回:'+RecCMD);
jo := SO(RecCMD);
IsSucc :=jo['result'].AsInteger;
if IsSucc=1 then //返回错误
Result := -1;
if IsSucc=0 then //成功
Result := 0;
except
on e : Exception do
begin
SaveFile_LB('---照片上传: 上传失败!:'+e.Message);
Result :=-2; //发送数据失败
Exit;
end;
end;
finally
postStream.Free;
ResponseStream.Free;
IdHttp.Free;
end;
end;问题: 照片字段根据JAVA代码 ,不知道Delphi这里如何转换了 ?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货