国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

通過url下載文件

2019-11-14 15:04:03
字體:
供稿:網(wǎng)友

1、問題簡介

  通過文件的url,將文件下載到本地。文件存儲(chǔ)的位置為:tomcat服務(wù)器的文件夾(通過讀取PRoperties文件:可看:http://www.survivalescaperooms.com/0201zcr/p/4700418.html)

2、實(shí)現(xiàn)思路

  讀取properties文件,將獲得文件將要存儲(chǔ)的位置

  通過java的Url類,將網(wǎng)上的文件下載到本地

3、代碼實(shí)現(xiàn)

1)、讀取properties文件(這里建立的是一個(gè)web project)

package com.zcr.until;import java.io.BufferedInputStream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.util.Properties;public class GetFilePlace {    /**     * 讀取文件,獲取保存的根目錄     * @return  保存的根目錄     */    public   String getFilePath(String fileProperties)    {        String dir = System.getProperty("user.dir");  //獲得tomcat所在的工作路徑                  //獲取到存儲(chǔ)了文件存儲(chǔ)位置的filedir.properties 文件路徑       // String realDir = dir + File.separator + "src" + File.separator +"META-INF" + File.separator + "config" + File.separator + "picture.properties";       String realDir = dir.substring(0, dir.length()-4) + File.separator +"webapps" + File.separator + "appDataGenerate" +File.separator + "WEB-INF"                + File.separator + "classes" + File.separator + "META-INF" + File.separator + "config" + File.separator + fileProperties;              /* String realDir = dir.substring(0, dir.length()-4) + File.separator +"webapps" + File.separator + "appDataGenerate"                       + File.separator + "classes" + File.separator + "META-INF" + File.separator + "config" + File.separator + fileProperties;    */        System.out.println("realDir = " + realDir);        return realDir;    }            /**     * 獲取filePath路徑【properities文件】中key對(duì)應(yīng)的值,     * @param filePath properities文件路徑【包含properities文件】     * @param key 要查找的key值     * @return key對(duì)應(yīng)的value     */     public   String GetValueByKey(String filePath, String key)      {         Properties pps = new Properties();         try {              InputStream in = new BufferedInputStream (new FileInputStream(filePath));                pps.load(in);             String value = pps.getProperty(key);             in.close();             return value;                      }catch (IOException e) {             e.printStackTrace();             return null;         }     }        /**     * 查詢properities文件中可以對(duì)應(yīng)的存儲(chǔ)地點(diǎn)     * @param key 查詢主鍵     * @return    key對(duì)應(yīng)的存儲(chǔ)地址     */    public  String getFileDirFromProperties(String key,String fileProperties)    {        return GetValueByKey(getFilePath(fileProperties),key);    }        public static void main(String[] args)    {        System.out.println(new GetFilePlace().getFileDirFromProperties("brandLogo","picture.properties"));    }}

 

2)、文件下載類

package com.zcr.until;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;import javax.servlet.http.HttpServletRequest;public class URLConnectionDownloader{    
//單純測(cè)試下載
public static void main(String[] args) { download("http://stocardapp.s3-external-3.amazonaws.com/ios/icons/1001tur@2x.png", "E://xiazai.jpg"); } /** * 將urlString的文件下載到 * @param filePathName properties文件中的文件存儲(chǔ)名 * @param fileProperties 查找的properties文件 * @param urlString 待下載的文件url * @param fileName 生成的文件名稱 */ public static void downloadToSelectedFolder(String filePathName,String fileProperties,String urlString,String fileName,HttpServletRequest request) { //獲得picture.properties 文件中,key為android_banner_url的值 String pathSavePath = new GetFilePlace().getFileDirFromProperties("android_banner_url","picture.properties"); //獲得服務(wù)器(tomcat)pathSavePath的相對(duì)位置 String path = request.getsession().getServletContext().getRealPath(pathSavePath); //獲得文件存儲(chǔ)的絕對(duì)路徑 String generateFileName = path + File.separator + fileName; download(urlString,generateFileName); } /** * 下載文件到本地 * * @param urlString * 被下載的文件地址 * @param filename * 本地文件名 */ public static void download(String urlString, String filename) { // 構(gòu)造URL URL url; try { url = new URL(urlString); // 打開連接 URLConnection con = url.openConnection(); // 輸入流 InputStream is = con.getInputStream(); // 1K的數(shù)據(jù)緩沖 byte[] bs = new byte[1024]; // 讀取到的數(shù)據(jù)長度 int len; // 輸出的文件流s OutputStream os = new FileOutputStream(filename); // 開始讀取 while ((len = is.read(bs)) != -1) { os.write(bs, 0, len); } // 完畢,關(guān)閉所有鏈接 os.close(); is.close(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

 

3)、網(wǎng)頁調(diào)用

URLConnectionDownloader.downloadToSelectedFolder("android_banner_url","picture.properties","http://stocardapp.s3-external-3.amazonaws.com/ios/icons/1001tur@2x.png","2x.png",request);

 

4)、測(cè)試結(jié)果

網(wǎng)頁的圖片:

下載的圖片

  致謝:感謝您的閱讀!


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 乌兰县| 思茅市| 德兴市| 阿合奇县| 全椒县| 瓮安县| 赤城县| 务川| 康马县| 合江县| 察雅县| 江阴市| 馆陶县| 攀枝花市| 平远县| 房产| 安泽县| 株洲市| 洛南县| 星座| 双城市| 陆河县| 赤水市| 电白县| 都匀市| 安徽省| 正安县| 巴东县| 宜兴市| 永靖县| 临武县| 南宫市| 八宿县| 克什克腾旗| 旬邑县| 滨海县| 宣汉县| 尖扎县| 仁布县| 株洲市| 石楼县|