国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
從URL獲取文件保存到本地的代碼
經(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://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);%>
<%@page import="java.net.*,java.io.*"%>
<%!
//54powerman
  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://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<BR>Get URL file " + flag);
%>
本站僅提供存儲服務,所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Java程序開發(fā)中如何應用線程詳解
使用HttpURLConnection訪問web頁面 - ┽JAVASE進階┾ - Java技術論壇|JavaSE|JavaEE|SCJP|SCWCD|SCEA|SCDJWS|SCBCD|java論壇 邁勝 - Powered by Discuz!
正則表達式學習經(jīng)驗分析
基于字符進行讀寫文件
java讀寫文件
Java IO簡介
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服