国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 太和县| 兰州市| 漳浦县| 台东县| 金溪县| 丰宁| 米林县| 莒南县| 邹城市| 子长县| 瑞金市| 九江市| 岳阳县| 晴隆县| 威远县| 高唐县| 嘉祥县| 日喀则市| 云浮市| 滨海县| 桃园市| 广安市| 镇沅| 焦作市| 丘北县| 灵丘县| 彭泽县| 封丘县| 洪泽县| 桂东县| 祁东县| 肥东县| 蓬溪县| 阆中市| 商城县| 兴和县| 莲花县| 莲花县| 连城县| 张家界市| 海淀区|