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

首頁 > 學院 > 開發設計 > 正文

從URL獲取文件保存到本地的代碼

2019-11-18 11:12:58
字體:
來源:轉載
供稿:網友

  <%@page import="java.net.*,java.io.*"%>

  <%!

   public boolean saveUrlAs(String photoUrl, String fileName) {

  //此方法只能用戶HTTP協議

   try {

   URL url = new URL(photoUrl);

   HttpURLConnection connection = (HttpURLConnection) url.openConnection();

   DataInputStream in = new DataInputStream(connection.getInputStream());

   DataOutputStream out = new DataOutputStream(new FileOutputStream(fileName));

   byte[] buffer = new byte[4096];

   int count = 0;

   while ((count = in.read(buffer)) > 0) {

   out.write(buffer, 0, count);

   }

   out.close();

   in.close();

   return true;

   }

   catch (Exception e) {

   return false;

   }

   }  

  public String getDocumentAt(String urlString) {

  //此方法兼容HTTP和FTP協議

   StringBuffer document = new StringBuffer();

   try {

   URL url = new URL(urlString);

   URLConnection conn = url.openConnection();

   BufferedReader reader = new BufferedReader(new InputStreamReader(conn.

   getInputStream()));

   String line = null;

   while ( (line = reader.readLine()) != null) {

   document.append(line + "/n");

   }

   reader.close();

   }

   catch (MalformedURLException e) {

   System.out.
   }

   catch (IOException e) {

   System.out.println("IOException when connecting to URL: " + urlString);

   }

   return document.toString();

   }

  %>

  <%

  //測試

   String photoUrl = "http://ad4.sina.com.cn/200601/12/43932_750450.jpg";

   String fileName = photoUrl.substring(photoUrl.lastIndexOf("/"));

   String filePath = "d:/
Ghost/";

   boolean flag = saveUrlAs(photoUrl, filePath + fileName);

   out.println("Run ok!/n
Get URL file " + flag);

  %>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 陇南市| 崇阳县| 军事| 遵义县| 巫山县| 邵阳市| 乳源| 婺源县| 长乐市| 大连市| 定襄县| 剑川县| 湖州市| 兴海县| 开鲁县| 蒙城县| 肥城市| 蒲江县| 北流市| 台北市| 台中市| 娄底市| 龙南县| 双鸭山市| 北安市| 高尔夫| 庆城县| 吉首市| 会同县| 天等县| 黄骅市| 金川县| 朝阳区| 山东省| 钦州市| 辽宁省| 渑池县| 盱眙县| 新野县| 筠连县| 苏尼特右旗|