通过url 下载文件

时间:2023-03-08 20:11:52

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)、测试结果

网页的图片:

通过url 下载文件

下载的图片

通过url 下载文件

  致谢:感谢您的阅读!