根据url,下载相应文件,并存储在本地,部分代码如下:
URL url; //声明url
InputStream inStream = null; //输入流
OutputStream outStream = null; //输出流
try {
url = new URL(strUrl); //获取url对象
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //连接url
conn.setRequestMethod("GET"); //连接方式
conn.setConnectTimeout(5000); //连接超时5秒
inStream = conn.getInputStream();
File dirPdf = new File(“E:\\spdb\\mmp\\pictures\\authorizeImges\\PDF”);//设置文件路径
if(!dirTiff.exists()) {
dirTiff.mkdirs();
}
File file; //定义写入文件位置
file = new File(dirPdf + "\\" + imageId + ".pdf");
/*配置写入文件位置*/
try {
outStream = new FileOutputStream(file); //包装流
} catch (FileNotFoundException e1) {
logger.error(e1);
}
/*写入文件,调用read()方法*/
byte[] b = new byte[1024];
int len;
while ((len = inStream.read(b)) != -1) {
outStream.write(b, 0, len);
}
} catch (MalformedURLException e) {
logger.error(e);
} catch (ProtocolException e) {
logger.error(e);
} catch (IOException e) {
logger.error(e);
}