Java 下载 HTTP 资源保存到本地

最近做个新功能,需要用到这个功能,测试无误,发上来备忘 :)


package  com.xxx.utility;

/*******************************************************************************
 * <p>Title: xxx</p>
 * <p>Description: xxx</p>
 * <p>Copyright: Copyright (c) 2006 DavidHsing All Rights Reserved</p>
 * <p>Company: xxx</p>
 * <p>WebSite: 
http://blog.csdn.net/DavidHsing
</p>
 *
 * 
@author
   : DavidHsing <DavidHsing(At)163.com>
 * 
@version
  : 1.00
 * @date     : 2007-08-28
 * @direction: 下载 HTTP 网络资源保存到本地
 *****************************************************************************
*/


import java.io.* ;
import java.net.*
;
import java.util.*
;

//******************************************************************************



public class  HttpDownload
{
    
//
==========================================================================

    
//
public HttpDownload() {}

    
//==========================================================================


    
private Vector vRemoteHttpURL = new Vector();
    
private Vector vLocalSaveFile = new
 Vector();

    
//==========================================================================


    
/**
     * 设置代理服务器
     * 
@param    String
     * 
@param
    String
     
*/

    
public void setProxy(String sProxyHost, String sProxyPort)
    
{
        
if (sProxyHost != null && !sProxyHost.trim().equals(""
))
        
{
            
if (sProxyPort == null || sProxyPort.trim().equals(""
))
            
{
                sProxyPort 
= "80"
;
            }

            System.getProperties().put(
"proxySet""true");
            System.getProperties().put(
"proxyHost"
, sProxyHost);
            System.getProperties().put(
"proxyPort"
, sProxyPort);
        }

    }


    
//==========================================================================

    
/**
     * 添加一个下载任务
     * 
@param    String
     * 
@param
    String
     * 
@return
   boolean
     
*/

    
public boolean addOneTask(String sRemoteHttpURL, String sLocalSaveFile)
    
{
        
if (sRemoteHttpURL == null || sRemoteHttpURL.trim().equals(""|| !sRemoteHttpURL.trim().substring(07).equalsIgnoreCase("http://"
))
        
{
            System.out.println(
" @> HttpDownload.addOneTask() : 源地址有误,不是一个有效的 http 地址!"
);
            
return false
;
        }

        
else
        
{
            
if (sLocalSaveFile == null || sLocalSaveFile.trim().equals(""
))
            
{
                sLocalSaveFile 
= "./" + sRemoteHttpURL.substring(sRemoteHttpURL.lastIndexOf("/"+ 1
);
            }

            vRemoteHttpURL.add(sRemoteHttpURL);
            vLocalSaveFile.add(sLocalSaveFile);
        }


        
return true;
    }


    
//==========================================================================

    
/**
     * 清除下载列表
     
*/

    
public void clearAllTasks()
    
{
        vRemoteHttpURL.clear();
        vLocalSaveFile.clear();
    }


    
//==========================================================================

    
/**
     * 根据列表下载资源
     * 
@return   boolean
     
*/

    
public boolean downLoadByList()
    
{
        
for (int i = 0; i < vRemoteHttpURL.size(); i++
)
        
{
            String sRemoteHttpURL 
=
 (String)vRemoteHttpURL.get(i);
            String sLocalSaveFile 
=
 (String)vLocalSaveFile.get(i);

            
if (!
saveToFile(sRemoteHttpURL, sLocalSaveFile))
            
{
                System.out.println(
" @> HttpDownload.downLoadByList() : 下载远程资源时出现异常!"
);
                
//return false;

            }

        }


        
return true;
    }


    
//==========================================================================

    
/**
     * 将 HTTP 资源保存为文件
     * 
@param    String
     * 
@param
    String
     * 
@return
   boolean
     
*/

    
private boolean saveToFile(String sRemoteHttpURL, String sLocalSaveFile)
    
{
        
if (sRemoteHttpURL == null || sRemoteHttpURL.trim().equals(""
))
        
{
            System.out.println(
" @> HttpDownload.saveToFile() : 要下载的远程资源地址不能为空!"
);
            
return false
;
        }


        
try
        
{
            URL tURL 
= new
 URL(sRemoteHttpURL);
            HttpURLConnection tHttpURLConnection 
=
 (HttpURLConnection)tURL.openConnection();
            tHttpURLConnection.connect();
            BufferedInputStream tBufferedInputStream 
= new
 BufferedInputStream(tHttpURLConnection.getInputStream());
            FileOutputStream tFileOutputStream 
= new
 FileOutputStream(sLocalSaveFile);

            
int nBufferSize = 1024 * 5
;
            
byte[] bufContent = new byte
[nBufferSize];
            
int nContentSize = 0
;
            
while ((nContentSize = tBufferedInputStream.read(bufContent)) != -1
)
            
{
                tFileOutputStream.write(bufContent, 
0
, nContentSize);
            }


            tFileOutputStream.close();
            tBufferedInputStream.close();
            tHttpURLConnection.disconnect();

            tURL 
= null;
            tHttpURLConnection 
= null
;
            tBufferedInputStream 
= null
;
            tFileOutputStream 
= null
;
        }

        
catch (Exception ex)
        
{
            System.out.println(
" @> HttpDownload.saveToFile() : 下载远程资源时出现异常!"
);
            System.out.println(
"    远程地址:" +
 sRemoteHttpURL);
            System.out.println(
"    本地路径:" +
 sLocalSaveFile);
            
return false
;
        }


        
return true;
    }


    
//==========================================================================

    
///**
    
//
 * 主方法(用于测试)
    
//
 * @param argv String[]
    
// */

    public static void main(String argv[])
    
{
        HttpDownload tHttpDownload 
= new
 HttpDownload();
        tHttpDownload.addOneTask("
http://www.baidu.com/test.zip""C:/test.zip"
);
        tHttpDownload.downLoadByList();
        tHttpDownload 
= null
;
    }


    
//==========================================================================

}
 // class HttpDownload end

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值