如何在Java中使用URL连接类发送FTP

  • 本文关键字:连接 FTP URL Java java url ftp
  • 更新时间 :
  • 英文 :


真的不确定为什么这不起作用。我无一例外地成功地完成了一次输出;但是,当我检查FTP存储库时,没有发送任何内容。由于在公司网络上,无法使用apache.commons,并且我的公司在其存储库中没有特定的库。

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Collection;

class SendAmpFeed {
public static void main(String[] args) {
uploadFTP();
}
public static void uploadFTP() {
final int BUFFER_SIZE = 4096;
String ftpURL = "ftp://anonymous:anonymous@blabla.web.blabla.com//xxx/yyyp;";
String filepath = "H:/myFolder/textFile.txt;
//ftpURL = String.format(ftpURL, user, pwd, hostname,uploadpath);
//System.out.println("Upload URL: " + ftpURL);
try {
URL url = new URL(ftpURL);
URLConnection conn = url.openConnection();
OutputStream outputStream = conn.getOutputStream();
FileInputStream inputStream = new FileInputStream(filepath);
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
System.out.println(inputStream.read());
}

System.out.println("File Successfully uploaded");
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Output: 
118
103
108
114
-1
File Successfully uploaded

不知道这是否有帮助,但几年前我为学校项目实现了一个简单的FTP客户端。你可以看一下代码,看看是否可以使用其中的部分

https://github.com/Kortex/Simple-FTP-Client

最新更新