........................

java 从http url下载文件的方法

上一篇 / 下一篇  2011-06-10 10:17:50 / 个人分类:java

package com.taobao.top.qatest;

import java.io.File;  
import java.net.URL;  

import org.apache.commons.io.FileUtils;  


import com.taobao.top.BaseCase;

 

public class Download{

  public static void main(String[] args) {   
    Download s=new Download();
        String dow = downloadFromUrl("http://3g.mobase.cn/download/uploadFiles/b5628yq10051430762.mp3","f:/");  
         System.out.println(dow);  
     }  
  
 
    public static String downloadFromUrl(String url,String dir) {  
  
        try {  
             URL httpurl = new URL(url);  
            String fileName = getFileNameFromUrl(url);  
            System.out.println(fileName);  
             File f = new File(dir + fileName);  
            FileUtils.copyURLToFile(httpurl, f);  
       } catch (Exception e) {  
             e.printStackTrace();  
             return "Fault ~~~>_<~~~+";  
         }   
         return "Successful (*^__^*)  ";  
     }             
    public static String getFileNameFromUrl(String url){  
        String name = new Long(System.currentTimeMillis()).toString() + ".X";  
        int index = url.lastIndexOf("/");  
        if(index > 0){  
             name = url.substring(index + 1);  
            if(name.trim().length()>0){  
                return name;  
            }  
         }  
         return name;  
     }  
 


TAG:

 

评分:0

我来说两句

Open Toolbar