您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Android图片异步上传到PHP服务器实例

2025/11/19 12:41:56发布17次查看
背景
网上很多上传到java服务器上的,找了好久,找到了上传到php的了,思路跟我当初想的差不多,就是post过去。废话不多说,直接上图看代码。
php代码
php代码
   android代码
上传的主要代码:
java代码
private   void  uploadfile(string uploadurl)     {       string end =  \r\n ;       string twohyphens =  -- ;       string boundary =  ****** ;        try        {         url url =  new  url(uploadurl);         httpurlconnection httpurlconnection = (httpurlconnection) url             .openconnection(); //http连接           // 设置每次传输的流大小,可以有效防止手机因为内存不足崩溃                    httpurlconnection.setchunkedstreamingmode( 128  *  1024 ); // 128k           // 允许输入输出流          httpurlconnection.setdoinput( true );         httpurlconnection.setdooutput( true );         httpurlconnection.setusecaches( false );          // 使用post方法          httpurlconnection.setrequestmethod( post );         httpurlconnection.setrequestproperty( connection ,  keep-alive ); //保持一直连接          httpurlconnection.setrequestproperty( charset ,  utf-8 ); //编码          httpurlconnection.setrequestproperty( content-type ,              multipart/form-data;boundary=  + boundary); //post传递过去的编码             dataoutputstream dos =  new  dataoutputstream(             httpurlconnection.getoutputstream()); //输出流          dos.writebytes(twohyphens + boundary + end);         dos.writebytes( content-disposition: form-data; name=\uploadedfile\; filename=\              + srcpath.substring(srcpath.lastindexof( / ) +  1 )             +  \              + end);         dos.writebytes(end);            fileinputstream fis =  new  fileinputstream(srcpath); //文件输入流,写入到内存中           byte [] buffer =  new   byte [ 8192 ];  // 8k           int  count =  0 ;          // 读取文件           while  ((count = fis.read(buffer)) != - 1 )         {           dos.write(buffer,  0 , count);         }         fis.close();            dos.writebytes(end);         dos.writebytes(twohyphens + boundary + twohyphens + end);         dos.flush();            inputstream is = httpurlconnection.getinputstream(); //http输入,即得到返回的结果          inputstreamreader isr =  new  inputstreamreader(is,  utf-8 );         bufferedreader br =  new  bufferedreader(isr);         string result = br.readline();            toast.maketext( this , result, toast.length_long).show(); //将结果输出          dos.close();         is.close();          }  catch  (exception e)       {         e.printstacktrace();         settitle(e.getmessage());       }     }   因为安卓4.0之后耗时间的操作要求都在非ui线程中操作,即将前面的asynctask拿来用了吧~
asynctask传送门: http://www.cnblogs.com/yydcdut/p/3707960.html
在这个类中,将上传的操作放在doinbackground当中,可以有progressdialog显示上传了多少:
java代码
// read file    bytesread = fileinputstream.read(buffer,  0 , buffersize);      while  (bytesread >  0 ) {       outputstream.write(buffer,  0 , buffersize);       length += buffersize;       progress = ( int ) ((length *  100 ) / totalsize);       publishprogress(progress);          bytesavailable = fileinputstream.available();       buffersize = math.min(bytesavailable, maxbuffersize);       bytesread = fileinputstream.read(buffer,  0 , buffersize);   }   outputstream.writebytes(lineend);   outputstream.writebytes(twohyphens + boundary + twohyphens           + lineend);   publishprogress( 100 );   还有就是,注意权限哟:
xml/html代码
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product