Chinaunix首页 | 论坛 | 博客
  • 博客访问: 820793
  • 博文数量: 754
  • 博客积分: 7000
  • 博客等级: 少将
  • 技术积分: 5005
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-12 12:54
文章分类

全部博文(754)

文章存档

2011年(1)

2008年(753)

我的朋友
最近访客

分类:

2008-09-12 13:12:48

  <%@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---------------------

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