微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

采用webservice接口上传大文件

原理很简单:用流的方式一次读取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 ? :)"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);

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐