采用webservice接口上传大文件
原理很简单:用流的方式一次读取2M的数据,然后上传2M数据,然后再接着读2M上传2M?依次类推?直到全部上传完成?下面是主要方法的实现? //一次读取2M的大小 //一次读取2M的大小 private?int?MAXLENTH=1024*1024*2; ? try?{ ???//发送到服务端????????????String?uploadBuffer?=?String(Base64.encode(b))????????????LinkedHashMap<String,Object>?hashMap=LinkedHashMap<>()hashMap.put("name"file.getName())"base64"uploadBuffer)"len"len)????????????if((totallen-readlen)==){????????????????isend?=?}????????????hashMap.put("isend"isend???:0?)"isfirst"isfirst?getResponseResult(hashMap)count+=; ???//更新UIMessage?msg=handler.obtainMessage()msg.what=0x88arg1=(int)(readlen/totallen*100.sendMessage(msg)if(readlen>=totallen){break;????}????Log.e("UoLoadFileTask""count=="+count)in.close()}?catch?(FileNotFoundException?e)?{????e.printStackTrace()Log."FileNotFoundException?e=="?+?e)(Exception?e){"Exception?e=="+e)} ? 下面贴上服务端的源码 ?public?String?dbxupload(String?name,String?base64,int?len,int?isend,int?isfirst){ System.out.println(name); if(isfirst==1){//第一次上传 upcache.remove(name); } BaseEntity?entity?=?new?BaseEntity(); FileOutputStream?out?=null; try?{ String?dirpath?=?XMConts.TMPDIR_windows +File.separator+"files"+File.separator+"dax/"; File?f?=?new?File(dirpath); if(!f.exists()) f.mkdirs(); if(!upcache.containsKey(name)){ out?=?new?FileOutputStream( ??new?File(dirpath,name)); upcache.put(name,?out); }else{ out?=?(FileOutputStream)?upcache.get(name); System.out.println("uu....."); byte[]?b?=?FileUtils.decodeBase64(base64); if(len>=0&&b.length>=0){ out.write(b,?0,?len); out.flush(); if(isend==1){//1是最后一个? out.close(); System.out.println("isend"); }?catch?(Exception?e)?{ e.printStackTrace(); entity.setCode(XMConts.ErrorCode.DB_ERR.getCode()); entity.setErrorDesc(e.getMessage()); if(out!=null){ }?catch?(IOException?ex)?{ ex.printStackTrace(); return?bean2Json(entity); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |