通過web url獲取檔案資訊

我就是曹總發表於2019-05-11

package com.js;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;

public class DownloadFile {

	/** 
     * @param args 
     */  
    public static void main(String[] args) { 
    	
    	List<String> urllist=new ArrayList<String>();
    	urllist.add("http://demo.18maret.com/demo/mimity/v1.3/css/bootstrap.css");
    	urllist.add("http://demo.18maret.com/demo/mimity/v1.3/css/font-awesome.min.css");
    	urllist.add("http://demo.18maret.com/demo/mimity/v1.3/css/jquery.bxslider.css");
    	urllist.add("http://demo.18maret.com/demo/mimity/v1.3/css/style.css");
    	urllist.add("http://demo.18maret.com/demo/mimity/v1.3/fonts/sourcesanspro-regular-webfont.woff");
    	urllist.add("http://demo.18maret.com/demo/mimity/v1.3/fonts/OpenSans-CondLight-webfont.woff");
    	urllist.add("http://demo.18maret.com/demo/mimity/v1.3/fonts/fontawesome-webfont.woff2");
    	urllist.add("http://demo.18maret.com/demo/mimity/v1.3/fonts/DroidSans-webfont.woff ");
    	urllist.add("http://demo.18maret.com/demo/mimity/v1.3/images/controls.png");
    	urllist.add("http://demo.18maret.com/demo/mimity/v1.3/images/bx_loader.gif ");
    	
    	for(int i=0;i<urllist.size();i++){
    		String res = downloadFromUrl(urllist.get(i).toString(),"E:/ttt/file/");  
            System.out.println(res);	
    	}
          
    }  
  
  
    /**
     * 通過url獲取檔案資訊
     * @param url
     * @param dir
     * @return
     */
    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;  
    }  
	
	
}



內容均為作者獨立觀點,不代表八零IT人立場,如涉及侵權,請及時告知。

相關文章