以下是JAVA呼叫的代碼:
public void doUploadFile() throws Exception
{
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()) > -1 ){bos.write(len);}
byte fileData [] = bos.toByteArray();
int tpsize1=fileData.length;
String urlstr="http://127.0.0.1:8080/uploadFile.tfc?method=savePicture&bh="+bh;
urlstr+= "&sj="+sj;
URL url = new URL(urlstr);
HttpURLConnection huc=(HttpURLConnection)url.openConnection();
huc.setRequestProperty("content-type", "text/html");
huc.setRequestProperty("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()));
String content="";
String line=br.readLine();
while(line!=null)
{
content=content+line;
line=br.readLine();
}
br.close();
}
請問上面的代碼如何用Delphi寫,我用的是Idhttp控制元件,就是圖片這部份傳不上去!
uj5u.com熱心網友回復:
下面是我自己寫的,圖片一直傳不上去filename:=ImagePath+'4035-367883-1.jpg';
RequestURL:=UploadPic+'?method=savePicture&bh=340800100024&sj=2017-09-04 08:36:05';
try
ms:=TMemoryStream.create;
ms.LoadFromFile(filename);
ms.SaveToStream(msa);
except
on e:exception do
begin
Memo1.lines.add(FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz',now)+':轉換圖片流出錯:'+e.message);
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-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:exception do
begin
Memo1.lines.add(FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz',now)+':提交圖片失敗:'+e.message);
end;
end;
IdHTTP.Free;
freeandnil(ms);
freeandnil(msb);
uj5u.com熱心網友回復:
影像檔案讀入流MS中,但未看見將MS上傳的代碼。uj5u.com熱心網友回復:
ResultUploadPic:=IdHTTP.post(TIdURI.URLEncode(RequestURL),ms);
多寫了一個a
uj5u.com熱心網友回復:
自己 找了一些資料,沒弄懂上傳圖片那塊://連接至URL
HttpURLConnection huc=(HttpURLConnection)url.openConnection();
//設定 訪問引數 ,與IDHTTP操作相反,IDHTTP設定引數在前,之后POST
huc.setRequestProperty("content-type", "text/html");
huc.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
huc.setDoOutput(true);
huc.setDoInput(true);
huc.setRequestMethod("POST");
huc.setConnectTimeout(500000);
//下面的陳述句應該是把圖片流上傳,但不知如何轉換為DELPHI陳述句
OutputStream pw=huc.getOutputStream();
pw.write(fileData);
pw.flush();
pw.close();
//這在JAVA中表示提交資料至服務端
BufferedReader br=new BufferedReader(new InputStreamReader(huc.getInputStream()));
uj5u.com熱心網友回復:

怎么沒人回呀,可以付費解決問題!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/50829.html
標籤:VCL組件開發及應用
