以下為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這里如何轉換了 ?
uj5u.com熱心網友回復:
可以把圖片轉成Base64來提交,后端再把Base64存成檔案轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/20770.html
標籤:網絡通信/分布式開發
上一篇:C++ 關鍵字decltype
下一篇:C++ 后置回傳型別
