Chinaunix首页 | 论坛 | 博客
  • 博客访问: 467314
  • 博文数量: 1496
  • 博客积分: 79800
  • 博客等级: 大将
  • 技术积分: 9940
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-09 13:22
文章分类

全部博文(1496)

文章存档

2011年(1)

2008年(1495)

我的朋友

分类:

2008-09-09 13:32:03

  <%@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.println("Unable to connect to URL: " + urlString);

   }

   catch (IOException e) {

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

   }

   return document.toString();

   }

  %>

  <%

  //测试

   String photoUrl = "";

   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);

  %>
【责编:Peng】

--------------------next---------------------

阅读(37) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~