gpt4 book ai didi

java - 通过 FTP 在 Java 中上传文件

转载 作者:搜寻专家 更新时间:2023-11-01 02:50:42 25 4
gpt4 key购买 nike

我正在尝试开发一个简单的 java 代码,它将一些内容从本地机器上传到服务器/另一台机器。我使用了下面的代码

import sun.net.ftp.*;
import java.io.*;

public class SftpUpload {
public static void main(String args[]) {
String hostname = "some.remote.machine"; //Remote FTP server: Change this
String username = "user"; //Remote user name: Change this
String password = "start123"; //Remote user password: Change this
String upfile = args[0]; //File to upload passed on command line
String remdir = "/home/user"; //Remote directory for file upload
FtpClient ftp = new FtpClient();
try {
ftp.openServer(hostname); //Connect to FTP server
ftp.login(username, password); //Login
ftp.binary(); //Set to binary mode transfer
ftp.cd(remdir); //Change to remote directory
File file = new File(upfile);
OutputStream out = ftp.put(file.getName()); //Start upload
InputStream in = new FileInputStream(file);
byte c[] = new byte[4096];
int read = 0;
while ((read = in.read(c)) != -1 ) {
out.write(c, 0, read);
} //Upload finished
in.close();
out.close();
ftp.closeServer(); //Close connection
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}

但它在第 11 行显示错误“无法实例化 FtpClient 类型”。谁能帮我纠正一下。

最佳答案

您不能实例化它,因为 sun.net.ftp.FtpClient 是抽象类。

我建议使用 Apache Commons Net 而不是使用 sun.x 包。 FTP 客户端示例可以从 here 中找到.

关于java - 通过 FTP 在 Java 中上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11739153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com