保存URL返回的文件

//文件名,System.getProperty下面说明
String filename = sdf.format(new Date()) + "_11.pdf"; 
String file = System.getProperty("cdncharge_pdf") + filename;
 URL urls = null; 
try { 
//url是网页路径
 urls = new URL(url); 
HttpURLConnection conn = (HttpURLConnection) urls.openConnection();
 conn.setConnectTimeout(5 * 1000); conn.setRequestMethod("GET");
 conn.setRequestProperty("Connetion", "Keep-Alive");
 InputStream inputStream = conn.getInputStream(); 
FileOutputStream f = new FileOutputStream(file); 
 byte[] buffer = new byte[1024]; 
 int count = 0; 
 while ((count = inputStream.read(buffer)) > 0) { 
 f.write(buffer, 0, count); 
 } 
 f.close();
 inputStream.close(); 
catch (Exception e) {
 e.printStackTrace(); 
 }
 
2.在xml文件里配置监听器,指向Java文件
public class ApplicationListener implements WebAppInit {
public void init(WebApp arg0) throws Exception {
String cdncharge_pdf = null;
cdncharge_pdf  = "D:/apache-tomcat-.0.32/webapps/cdncharge/cdndomaingrouppdf/";
File file = new File(cdncharge_pdf );
if(!file.exists()){
logger.info("cdncharge path not exists make dirs completed");
file.mkdirs();
}
System.setProperty("cdncharge_pdf", cdncharge_pdf);
....................................
}
}
posted @ 2015-09-07 16:07  kisstear  阅读(263)  评论(0编辑  收藏  举报