久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

遠(yuǎn)程下載文件

 溈伱 2010-03-29

/*
 * Created on 2007-4-10
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.dreamwin.util;


import java.io.*;
import java.net.*;
/**
 * 作者:謝榮華 日期:2007-4-10 功能:功能描敘
 * 
 */
public class GetFileFromHTTP
{
    private long totalSize=0l;
    private final String tempfix="tp";   

    //確定文件是否已經(jīng)下載,但沒(méi)有下載完成
    public boolean checkfileExist(String filepath)
    {
      File file = new File(filepath);    
      if (file.exists())
      {
          return true; 
      }    
      return false;     
    }
  
    //確定已經(jīng)下載了的文件大小
    public long getFileSize(String filepath)
    {
      File file = new File(filepath);    
      return file.length();
    }   
   
    public String getTpFileName(String filepath)
    {
      int index=filepath.lastIndexOf(".");
      if(index!=-1)
      {
          return filepath.substring(0,index)+"."+tempfix;          
      }
      return filepath;
    }
   
    private void FileRename(String fName,String nName)
    {
      File file = new File(fName);
      file.renameTo(new File(nName));
      file.delete();
    }   
   
    //獲取文件流
    public boolean getFile(String srcpath,String despath)
    {
        String tpdespath=getTpFileName(despath);
        System.out.println("[文件下載開(kāi)始]來(lái)源="+srcpath);
        try
        {       
            URL l_url = new java.net.URL(srcpath);
            HttpURLConnection l_connection =(HttpURLConnection)l_url.openConnection();
            l_connection.connect();
            totalSize=Long.parseLong(l_connection.getHeaderField("Content-Length"));
            System.out.println("[文件大小]="+totalSize); 
           
            int len=0;
            int totallen=0;
            byte[] bt = new byte[1024];
           
            if (l_connection.getResponseCode() == HttpURLConnection.HTTP_OK)
            {  
                if(checkfileExist(tpdespath))//采用斷點(diǎn)續(xù)傳,這里的依據(jù)是看下載文件是否在本地有.tp有擴(kuò)展名同名文件
                {                    
                   System.out.println("[斷點(diǎn)續(xù)傳]...");
                   long fileSize =getFileSize(tpdespath); //取得文件在小,,以便確定隨機(jī)寫入的位置
                   System.out.println("[已有大小]="+fileSize);
                   l_connection.disconnect();
                   l_connection =(HttpURLConnection)l_url.openConnection();
                   l_connection.setRequestProperty("RANGE", "bytes="+fileSize+"-");                       
                   RandomAccessFile  raFile=new RandomAccessFile(tpdespath,"rw");//隨機(jī)方位讀取
                   raFile.seek(fileSize); 
                   BufferedInputStream  bis = new BufferedInputStream(l_connection.getInputStream());                 
                   while ((len = bis.read(bt)) > 0)
                   {
                     totallen=totallen+len;
                     raFile.write(bt,0,len);
                     long progress=((totallen*100)/totalSize);
                     System.out.println("[下載進(jìn)度=]"+progress+"%"); 
                   }
                   raFile.close();
                   bis.close(); 
                } 
                else
                {
                    System.out.println("[新建傳輸]...");
                    FileOutputStream fos = new FileOutputStream(tpdespath);              
                    DataOutputStream  dos = new DataOutputStream(fos);               
                    BufferedInputStream  bis = new BufferedInputStream(l_connection.getInputStream());
                   
                 while ((len=bis.read(bt)) > 0)
                 {
                   dos.write(bt,0,len); 
                   totallen=totallen+len;                
                   long progress=((totallen*100)/totalSize);
                   System.out.println("[下載進(jìn)度=]"+progress+"%");                 
                 } 
                 bis.close();
                 dos.close();
                 fos.close();  
                }                          
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }   
       
        if(totalSize==getFileSize(getTpFileName(despath)))
        {
          FileRename(getTpFileName(despath),despath);
          System.out.println("[文件下載完成]保存="+despath);
        }         
        return true;
    }

    public static void main(String args[])
    {
        GetFileFromHTTP gf=new GetFileFromHTTP();
        String srcpath="http://59.38.125.122/uploadfile/vodone/97d36f6cf62208bcf8ce21c065355d48/97d36f6cf62208bcf8ce21c065355d48.wmv";
        String despath="E://01.wmv";      
        gf.getFile(srcpath,despath);
    }
}

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,,所有內(nèi)容均由用戶發(fā)布,,不代表本站觀點(diǎn),。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式,、誘導(dǎo)購(gòu)買等信息,,謹(jǐn)防詐騙,。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào),。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多