Home > Back-end >  Java HTTP upload conversion Delphi
Java HTTP upload conversion Delphi

Time:11-09

The following is a JAVA code
Public void uploadBusinessImg3 () throws the Exception {
String host="http://183.134.253.23:8086/nbyd-parkpot-service-test/";
The String url=host + "parkpot/uploadBusinessImg";
HttpPost HttpPost=new HttpPost (url);

MultipartEntityBuilder entityBuilder=MultipartEntityBuilder. The create ();

The File File=new File (" F:/a. pg ");
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");//business types (1:2 to: leave)
DataObject. Put (" takeTime ", DateUtil getDate (new Date (), 12));//photo time
DataObject. Put (" imgIndex ", 2);//photo index
DataObject. Put (" imageHash ", "055 ec8012312ad5b54d94b3e91f5633e");
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. The build ();
HttpPost. SetEntity (entity);

CloseableHttpClient httpClient=HttpClients. CreateDefault ();
CloseableHttpResponse httpResponse=httpClient. Execute (httpPost);

The String result=EntityUtils. ToString (httpResponse. GetEntity ());
System. The out. Println (" result: "+ result);
}



The following for Delphi code:
The Function LB_uploadBusinessImg (/5.4/car business photo
PicFile: String;//file path
Businessid: String;//parking business number (business Numbers for a vehicle to leave the business ID) is
Businesstype: String;//business types (1: reach 2: leave) is
ImgIndex: Integer;/photo/index (each business, most of the same type of business can only upload photo 4, which reached 2, leave 2, when uploading imgIndex greater than 5, the system will not receive the photo) is
TakeTime: the String//format: yyyyMMddHHmmss is
) : Integer;
Var
PostStream: TIdMultiPartFormDataStream;
IdHttp: TIdHTTP;
ResponseStream: TStringStream;//return information
RecCMD IMSUrl: String;
MyMD5: TIdHashMessageDigest5;
IMSCMD, signs and FileMD5: String;
MyFile: TFileStream;
Jo: ISuperObject;
IsSucc: Integer;

The begin
IF RunParam. LBSC=0 THEN the Exit;
If the Copy (RunParam LBDZ, Length (RunParam. LBDZ) - 1, 1) & lt;> '/' then
IMSUrl:=RunParam LBDZ + '/parkpot uploadBusinessImg'
The else
IMSUrl:=RunParam LBDZ + 'parkpot/uploadBusinessImg';
MyMD5:=TIdHashMessageDigest5. Create;
MyFile:=TFileStream. Create (PicFile fmOpenRead);
TRY
//the file MD5
Try
FileMD5:=MyMD5 AsHex (MyMD5 HashValue (MyFile));
Except,
Result:=3;
SaveFile_LB (' error - error: MD5 photos! -- ');
The Exit;
end;
The finally
MyFile. Free;
MyMD5. Free;
end;

IdHttp:=TIdHTTP. Create (nil);//create IDHTTP control
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 (PicFile 'file', 'image/jpeg);//form file
Try
IdHttp. Post (IMSUrl postStream, ResponseStream);

//web pages returned by the information
RecCMD:=ResponseStream. DataString;
//when there is a Chinese web page of the need for UTF8 decoding
RecCMD:=UTF8Decode (RecCMD);
SaveFile_LB (' - photo upload: upload return: '+ RecCMD);
Jo: SO=(RecCMD);
IsSucc: Jo=[' result '] AsInteger;
If IsSucc=1 then//return error
Result:=1;
If IsSucc=0 then//success
Result:=0;
Except,
On e: the Exception do
The begin
SaveFile_LB (' - photo upload: upload failed! : "+ e.M essage);
Result:=2;//send data failure
The Exit;
end;
end;
The finally
PostStream. Free;
ResponseStream. Free;
IdHttp. Free;
end;
end;

Problem: photo fields according to the JAVA code, I don't know how to convert the Delphi here?

CodePudding user response:

Can put the picture into Base64 to submit, the back-end to deposit the Base64 into a file
  • Related