Topic: JAVA中如何执行FTP命令?

  Print this page

1.JAVA中如何执行FTP命令? Copy to clipboard
Posted by: wangfeng7838
Posted on: 2004-11-23 13:20

请问高手,如何在JAVA中执行FTP协议中的命令?急,谢了先!

2.Re:JAVA中如何执行FTP命令? [Re: wangfeng7838] Copy to clipboard
Posted by: wangfeng7838
Posted on: 2004-11-23 13:21


3.Re:JAVA中如何执行FTP命令? [Re: wangfeng7838] Copy to clipboard
Posted by: Jove
Posted on: 2004-11-23 13:39

了解FTP协议,直接操作套接字
或找个现成lib,如 http://jakarta.apache.org/commons/net/

4.Re:JAVA中如何执行FTP命令? [Re: wangfeng7838] Copy to clipboard
Posted by: jameszhang
Posted on: 2004-11-23 15:27

sun.net.ftp.FtpClient

5.Re:JAVA中如何执行FTP命令? [Re: wangfeng7838] Copy to clipboard
Posted by: Lawme
Posted on: 2004-11-23 16:53

Java FTP 的关键是建立 URL 时的语法。本例中是这句:
URL url = new URL(args[1]);

这里的 args[1] 结构必须为:

ftp:// user : password @ FtpUrl

例如:
ftp://someone:m3j4v8@dongfang.com/pages/index.html

以下是段 FTP 上传文件的代码:

import java.io.*;
import java.net.*;

public class FtpUpload {

public static void main(String[] args) throws Exception {

if(args.length < 2){
System.out.println("\n用法:java UrlUp <源文件名> <目标文件名>\n");
return;
}

int n1,n2,n3;
String s = args[0];

n1 = s.indexOf(".htm");
n2 = s.indexOf(".html");
n3 = s.indexOf(".txt");

if( ( n1 + n2 + n3 ) > -3 ){ // 源文件是文本文件

BufferedReader in = new BufferedReader(new FileReader(args[0]));

URL url = new URL(args[1]);
URLConnection connection = url.openConnection();
connection.setDoOutput(true);

PrintWriter out = new PrintWriter( connection.getOutputStream());

String inputLine;
while ((inputLine = in.readLine()) != null)
out.write(inputLine+"\n");

in.close();
out.close();
}
else{ // 源文件是非文本的,如图像、声音等

File bin = new File(args[0]);
long size = bin.length();
byte[] buffer = new byte[(int)size];

FileInputStream in = new FileInputStream(args[0]);
in.read(buffer);

URL url = new URL(args[1]);
URLConnection connection = url.openConnection();
connection.setDoOutput(true);

OutputStream out = connection.getOutputStream();
out.write(buffer);

in.close();
out.close();

}

}
}



   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923