1、問題簡介
通過文件的url,將文件下載到本地。文件存儲的位置為:tomcat服務器的文件夾(通過讀取properties文件:可看:http://www.cnblogs.com/0201zcr/p/4700418.html)
2、實現思路
讀取properties文件,將獲得文件將要存儲的位置
通過java的Url類,將網上的文件下載到本地
3、代碼實現
1)、讀取properties文件(這里建立的是一個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所在的工作路徑 //獲取到存儲了文件存儲位置的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對應的值, * @param filePath properities文件路徑【包含properities文件】 * @param key 要查找的key值 * @return key對應的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文件中可以對應的存儲地點 * @param key 查詢主鍵 * @return key對應的存儲地址 */ 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 {
//單純測試下載 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文件中的文件存儲名 * @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"); //獲得服務器(tomcat)pathSavePath的相對位置 String path = request.getSession().getServletContext().getRealPath(pathSavePath); //獲得文件存儲的絕對路徑 String generateFileName = path + File.separator + fileName; download(urlString,generateFileName); } /** * 下載文件到本地 * * @param urlString * 被下載的文件地址 * @param filename * 本地文件名 */ public static void download(String urlString, String filename) { // 構造URL URL url; try { url = new URL(urlString); // 打開連接 URLConnection con = url.openConnection(); // 輸入流 InputStream is = con.getInputStream(); // 1K的數據緩沖 byte[] bs = new byte[1024]; // 讀取到的數據長度 int len; // 輸出的文件流s OutputStream os = new FileOutputStream(filename); // 開始讀取 while ((len = is.read(bs)) != -1) { os.write(bs, 0, len); } // 完畢,關閉所有鏈接 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)、網頁調用
URLConnectionDownloader.downloadToSelectedFolder("android_banner_url","picture.properties","http://stocardapp.s3-external-3.amazonaws.com/ios/icons/1001tur@2x.png","2x.png",request);
4)、測試結果
網頁的圖片:
下載的圖片
致謝:感謝您的閱讀!