FTP~~URL对FTP的支持原因

下面的例子,只是让自己对FTP有一个简单的认识,没有多大的意义,自己练习一下.同时发现了原来JDK6.0以及将jarkarta的net框架收入,URL中对FTP的支持就是使用这个框架啊!

FTP的URL格式如下

ftpurl         = “ftp://” login [ “/” fpath [ “;type=” ftptype ]]
fpath          = fsegment *[ “/” fsegment ]
fsegment       = *[ uchar | “?” | “:” | “@” | “&” | “=” ]
ftptype        = “A” | “I” | “D” | “a” | “i” | “d”

简单示例

ftp://<用户名>:<密码>@<主机名>/文件URL;type=<FTP类型>

ftp://ftp.neu.edu.cn/bt.neu6.edu.cn.txt 其中bt.neu6.edu.cn.txt是文件名,

下面的代码就可以用于下载此文件
  1. URL url = new URL("ftp://ftp.neu.edu.cn/bt.neu6.edu.cn.txt");
  2. URLConnection con = url.openConnection();
  3. InputStream in = con.getInputStream();
  4. String r = inputstreamToString(in);
  5. System.out.println(r);
  6.     private static String inputstreamToString(InputStream in) {
  7.         BufferedReader reader = new BufferedReader(new InputStreamReader(in));
  8.         StringBuffer buffer = new StringBuffer();
  9.         String str = null;
  10.         try {
  11.             while ((str = reader.readLine()) != null) {
  12.                 buffer.append(str + "\r\n");
  13.             }
  14.             return buffer.toString();
  15.         } catch (IOException e) {
  16.             return buffer.toString();
  17.         }
  18.     }

下面是实现上传

  1.     URL url = new URL("ftp://ftp.neu.edu.cn/netinstall/1.txt");// 要上传的文件地址
  2.             URLConnection con = url.openConnection();
  3.             con.getOutputStream().write("文件内容".getBytes("GB2312"));
  4.             con.getOutputStream().close();

由于这个是我们学校的FTP服务器,会抛出异常,这就让我知道了原来URL对FTP的支持也是使用jarkarta的net框架,也就是说JDK6.0已经收入了这个框架了。异常信息如下

sun.net.ftp.FtpProtocolException: 553 Anonymous users may not overwrite existing files

 at sun.net.ftp.FtpClient.openDataConnection(Unknown Source)
 at sun.net.ftp.FtpClient.put(Unknown Source)
 at sun.net.www.protocol.ftp.FtpURLConnection.getOutputStream(Unknown Source)
 at Test.Test.main(Test.java:24)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值