当前位置: 移动技术网 > IT编程>开发语言>Java > java断点续传功能实例(java获取远程文件)

java断点续传功能实例(java获取远程文件)

2019年07月22日  | 移动技术网IT编程  | 我要评论

复制代码 代码如下:

import java.io.bufferedinputstream;
 import java.io.dataoutputstream;
 import java.io.file;
 import java.io.fileoutputstream;

import java.io.ioexception;

import java.io.randomaccessfile;

import java.net . * ;

/**
* 文件传送客户端:获取远程文件
 */
public class getremotefile_client_goon { public getremotefile_client_goon()
   {
   }

  private boolean fileexist(string pathandfile) // 确定文件是否已经下载,但没有下载完成
  {
     file file = new file(pathandfile);
     if (file.exists())
       return true ;
     else
      return false ;
   }

  private long filesize(string pathandfile) // 确定已经下载了的文件大小
  {
     file file = new file(pathandfile);
     return file.length();
   }
   private void filerename(string fname,string nname) // 将下载完全的文件更名,去掉.tp名
  {
     file file = new file(fname);
     file.renameto( new file(nname));
     file.delete();
   }
   public static void main(string[] args)
   {
     url url = null ;
     httpurlconnection urlc = null ;
     dataoutputstream dos = null ;
     bufferedinputstream bis = null ;
     fileoutputstream fos = null ;
     string localfile = " d:////x.x " ; // 文件保存的地方及文件名,具体情况可以改
    string localfile_bak = localfile + " .tp " ; // 未下载完文件加.tp扩展名,以便于区别
    getremotefile_client_goon gco = new getremotefile_client_goon();
     long filesize = 0 ;
     long start = system.currenttimemillis();
     int len = 0 ;
     byte [] bt = new byte [ 1024 ];
     // byte[] buffer=new byte[50*1024];
    randomaccessfile rafile = null ;
     long totalsize = 0 ; // 要下载的文件总大小
    try
    {
       url = new url( " http://www.netbox.cn/download/nbsetup.exe " );     
      urlc = (httpurlconnection) url.openconnection();
       totalsize = long.parselong(urlc.getheaderfield( " content-length " ));
       system.out.println( " 下载文件大小为: " + totalsize);
       urlc.disconnect(); // 先断开,下面再连接,否则下面会报已经连接的错误
      urlc = (httpurlconnection) url.openconnection();
       // 确定文件是否存在
      if (gco.fileexist(localfile_bak)) // 采用断点续传,这里的依据是看下载文件是否在本地有.tp有扩展名同名文件
      {
         system.out.println( " 文件续传中… " );
         filesize = gco.filesize(localfile_bak); // 取得文件在小,以便确定随机写入的位置
        system.out.println( " filesize: " + filesize);
         // 设置user-agent
         // urlc.setrequestproperty("user-agent","netfox");
         // 设置断点续传的开始位置
        urlc.setrequestproperty( " range " , " bytes= " + filesize + " - " );
         // urlc.setrequestproperty("range", "bytes="+filesize); // 这样写不行,不能少了这个"-".
         // 设置接受信息
        urlc.setrequestproperty( " accept " , " image/gif,image/x-xbitmap,application/msword,*/* " );       
        rafile = new randomaccessfile(localfile_bak, " rw " ); // 随机方位读取
        rafile.seek(filesize); // 定位指针到filesize位置
        bis = new bufferedinputstream(urlc.getinputstream());
         while ((len = bis.read(bt)) > 0 ) // 循环获取文件
        {
           rafile.write(bt, 0 , len);
           // buffer=buffer+bt;
           // system.
        }
         system.out.println( " 文件续传接收完毕! " );       
      }
       else // 采用原始下载
      {
         fos = new fileoutputstream(localfile_bak); // 没有下载完毕就将文件的扩展名命名.bak
        dos = new dataoutputstream(fos);
         bis = new bufferedinputstream(urlc.getinputstream());       
        system.out.println( " 正在接收文件… " );
         int test = 0 ;
         while ((len = bis.read(bt)) > 0 ) // 循环获取文件
        {
           dos.write(bt, 0 , len);
           test ++ ;
           if (test == 50 ) // 这里是测试,你可以删除这里,就可以正常下载了
            break ;
         }       
        // system.out.println("文件正常接收完毕!");
      }     
      system.out.println( " 共用时: " +
                         (system.currenttimemillis() - start) / 1000 );
       if (bis != null )
         bis.close();
       if (dos != null )
         dos.close();
       if (fos != null )
         fos.close();
       if (rafile != null )
         rafile.close();
       system.out.println( " localfile_bak: " + gco.filesize(localfile_bak));
       if (gco.filesize(localfile_bak) == totalsize) // 下载完毕后,将文件重命名
      {
         gco.filerename(localfile_bak,localfile);
       }
       system.exit( 0 );
     }
     catch (exception e)
     {
       try
      {
         if (bis != null )
           bis.close();
         if (dos != null )
           dos.close();
         if (fos != null )
           fos.close();
         if (rafile != null )
           rafile.close();
       }
       catch (ioexception f)
       {
         f.printstacktrace();
       }
       e.printstacktrace();
     }
     system.exit( 0 );
   }
 }

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网