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

首頁 > 開發(fā) > 綜合 > 正文

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

2024-07-21 02:14:45
字體:
供稿:網(wǎng)友
 
經(jīng)常用,先放這里,用的時候過來拿!
 
<%@page import="java.net.*,java.io.*"%>
<%!
  public boolean saveurlas(string photourl, string filename) {
//此方法只能用戶http協(xié)議
    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協(xié)議
    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.println("unable to connect to url: " + urlstring);
    }
    catch (ioexception e) {
      system.out.println("ioexception when connecting to url: " + urlstring);
    }
    return document.tostring();
  }
%>
<%
//測試
  string photourl = "http://www.alixixi.com/uploadpic/2007-7/200777112920214.jpg";
  string filename = photourl.substring(photourl.lastindexof("/"));
  string filepath = "d:/ghost/";
  boolean flag = saveurlas(photourl, filepath + filename);
  out.println("run ok!/n<br>get url file " + flag);
%>
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 托克逊县| 襄城县| 永清县| 纳雍县| 高碑店市| 富川| 泸水县| 鄂托克旗| 尼勒克县| 瑞丽市| 西乌| 贡觉县| 贵溪市| 宝清县| 怀安县| 鹤峰县| 葫芦岛市| 南通市| 客服| 三台县| 通州市| 呈贡县| 怀集县| 霍邱县| 饶河县| 乡城县| 濮阳市| 赣榆县| 偏关县| 闽清县| 张北县| 兴和县| 宝鸡市| 买车| 定远县| 浮梁县| 台中市| 许昌市| 旬邑县| 青川县| 甘孜|