delphi 调用Servlet用于图片上传,图片为二进制输入流

zxp922 2017-09-04 03:47:11
以下是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控件,就是图片这部份传不上去!
...全文
389 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zxp922 2017-09-06
  • 打赏
  • 举报
回复
怎么没人回呀,可以付费解决问题!
zxp922 2017-09-05
  • 打赏
  • 举报
回复
自己 找了一些资料,没弄懂上传图片那块: //连接至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()));
zxp922 2017-09-05
  • 打赏
  • 举报
回复
引用 2 楼 lyhoo163 的回复:
图像文件读入流MS中,但未看见将MS上传的代码。
ResultUploadPic:=IdHTTP.post(TIdURI.URLEncode(RequestURL),ms); 多写了一个a
lyhoo163 2017-09-04
  • 打赏
  • 举报
回复
图像文件读入流MS中,但未看见将MS上传的代码。
zxp922 2017-09-04
  • 打赏
  • 举报
回复
下面是我自己写的,图片一直传不上去 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);

5,391

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧