加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

采用webservice接口上传大文件

发布时间:2020-12-16 22:25:33 所属栏目:安全 来源:网络整理
导读:原理很简单:用流的方式一次读取2M的数据,然后上传2M数据,然后再接着读2M上传2M?依次类推?直到全部上传完成 ?下面是主要方法的实现? //一次读取2M的大小 //一次读取2M的大小 private?int?MAXLENTH=1024*1024*2; ? try? { ???? long? totallen?=?file.length

原理很简单:用流的方式一次读取2M的数据,然后上传2M数据,然后再接着读2M上传2M?依次类推?直到全部上传完成?下面是主要方法的实现?

//一次读取2M的大小

//一次读取2M的大小

private?int?MAXLENTH=1024*1024*2;

?

try?{
????long?totallen?=?file.length();InputStream?in?=?new?FileInputStream(file)????byte?b[]?=?new?byte[MAXLENTH]????int?len?=?0????float?readlen?=?count=//上传了多少次????boolean?isfirst=true;//服务端接口参数isend?=?false;????while((len=in.read(b))!=-1){????//当没有读取完时,继续读取?????????readlen+=len????????{

???//发送到服务端????????????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);

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读