Home > Back-end >  Delphi to invoke the Servlet is used to upload images, image to binary input stream
Delphi to invoke the Servlet is used to upload images, image to binary input stream

Time:09-18

The following is the code for the JAVA calls:
Public void doUploadFile () throws the Exception
{
The String file="E:/044160004044 JPG";
String bh="430600100063";
String sj="2017-09-02 11:07:23";
ByteArrayOutputStream bos=new ByteArrayOutputStream ();
BufferedInputStream bis=new BufferedInputStream (new FileInputStream (file));
Int len.
While ((len=bis. Read ()) & gt; 1) {bos. Write (len); }
Byte fileData []=bos. ToByteArray ();
Int tpsize1=fileData. Length;
String urlstr="HTTP://http://127.0.0.1:8080/uploadFile.tfc? Method=savePicture& Bh="+ bh.
Urlstr +="& amp; Sj="+ sj.
URL the URL=new URL (urlstr);
HttpURLConnection huc=(HttpURLConnection) url. OpenConnection ();
Huc. SetRequestProperty (" the content-type ", "text/HTML");
Huc. SetRequestProperty (" the content-type ", "application/x - WWW - form - urlencoded; charset=utf-8");
Huc. SetDoOutput (true);
Huc. SetDoInput (true);
Huc. SetRequestMethod (" POST ");
Huc. SetConnectTimeout (500000);
OutputStream pw=huc. GetOutputStream ();
Pw. Write (fileData);
Pw. Flush ();
pw.close();
BufferedReader br=new BufferedReader (new InputStreamReader (huc. GetInputStream ()));
The String content="";
The String line=br. ReadLine ();
While (the line!=null)
{
The content=content + line;
The line=br. ReadLine ();
}
br.close();
}


How do the above code written in Delphi, I use Idhttp controls, picture is the copy of transfer will not come!

CodePudding user response:

The following was written by myself, images have been preach not up

The filename: ImagePath +='4035-367883-1. JPG';
The RequestURL: UploadPic +='? Method=savePicture& Bh=340800100024 & amp; Sj=2017-09-04 08:36:05 ';
Try
Ms:=TMemoryStream. Create;
Ms. LoadFromFile (filename);
Ms. SaveToStream (msa);
Except,
On e: the exception do
The begin
Memo1. Lines. The add (FormatDateTime (' yyyy - - dd hh: mm nn: ss, z ', now) + ': convert pictures out wrong:' + e.m essage);
end;
end;

IdHTTP:=TIdHTTP. Create (nil);
IdHTTP. ReadTimeout:=10000;
IdHTTP. AllowCookies:=True;
IdHTTP. Request. ContentType:='application/x - WWW - form - urlencoded; Charset=utf-8 ';//application/x - WWW - form - urlencoded
IdHTTP. HTTPOptions:=[hoForceEncodeParams];
IdHTTP. Request. Accept:='text/HTML, image/GIF image/jpeg, *; Q=2, */*; Q=. 2 ';
IdHTTP. Request. UserAgent:='Mozilla/3.0 (compatible; Indy Library) ';
Idhttp. Request. ContentType:='keep - the alive;
Idhttp. Request. CacheControl:='no - cache;
Idhttp. Request. Pragma:='no - cache;
Idhttp. Request. ContentLength:=Ms. Size;
Idhttp. Request. Host: ServerIP=+ + ServerPort ':';

Try
ResultUploadPic:=IdHTTP. Post (TIdURI URLEncode (RequestURL), msa);
Except,
On e: the exception do
The begin
Memo1. Lines. The add (FormatDateTime (' yyyy - - dd hh: mm nn: ss, z ', now) + ': submit pictures failure: + e.m essage);
end;
end;

IdHTTP. Free;
Freeandnil (ms).
Freeandnil (MSB);

CodePudding user response:

Inflow of MS image file to read, but did not see MS upload code,

CodePudding user response:

refer to the second floor lyhoo163 response:
inflow in the MS image file to read, but did not see will upload code, MS


ResultUploadPic:=IdHTTP. Post (TIdURI URLEncode (RequestURL), ms);

Write an a

CodePudding user response:

Oneself to find some information, didn't understand to upload pictures that block:
//to connect to the URL
HttpURLConnection huc=(HttpURLConnection) url. OpenConnection ();
//set access parameters, contrary to IDHTTP operation, IDHTTP set parameters in the former, after POST
Huc. SetRequestProperty (" the content-type ", "text/HTML");
Huc. SetRequestProperty (" the content-type ", "application/x - WWW - form - urlencoded; charset=utf-8");
Huc. SetDoOutput (true);
Huc. SetDoInput (true);
Huc. SetRequestMethod (" POST ");
Huc. SetConnectTimeout (500000);
//the following statement should be flow to upload the pictures, but I don't know how to convert the DELPHI statement
OutputStream pw=huc. GetOutputStream ();
Pw. Write (fileData);
Pw. Flush ();
pw.close();
//this said to submit data to the server in JAVA
BufferedReader br=new BufferedReader (new InputStreamReader (huc. GetInputStream ()));

CodePudding user response:



How no one back ah, can pay to solve the problem!
  • Related