我在JAVA中使用此代码
FTPClient client = new FTPClient();
FileInputStream fis = null;
try
{
client.connect("ftp.serverapp.amon-internet.com");
client.login("u780746385", "serverapp");
//client.changeWorkingDirectory("/");
//client.setFileType(org.apache.commons.net.ftp.FTP.ASCII_FILE_TYPE);
//client.enterLocalPassiveMode();
//
// Create an InputStream of the file to be uploaded
//
String filename = "test.txt";
fis = new FileInputStream(filename);
//
// Store file to server
//
client.storeFile(filename, fis);
client.logout();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (fis != null)
{
fis.close();
}
client.disconnect();
}
catch (IOException e)
{
e.printStackTrace();
}
}
所以它可以连接,但是当它试图上传我的文件时,它会说:
java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at sun.nio.cs.StreamEncoder.writeBytes(Unknown Source)
at sun.nio.cs.StreamEncoder.implFlushBuffer(Unknown Source)
at sun.nio.cs.StreamEncoder.implFlush(Unknown Source)
at sun.nio.cs.StreamEncoder.flush(Unknown Source)
at java.io.OutputStreamWriter.flush(Unknown Source)
at java.io.BufferedWriter.flush(Unknown Source)
at org.apache.commons.net.ftp.FTP.__send(FTP.java:505)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:479)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:608)
at org.apache.commons.net.ftp.FTP.port(FTP.java:932)
at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:812)
at org.apache.commons.net.ftp.FTPClient._storeFile(FTPClient.java:633)
at org.apache.commons.net.ftp.FTPClient.__storeFile(FTPClient.java:624)
at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:1976)
at Waar.Waar.<init>(Waar.java:67)
at Waar.Waar.main(Waar.java:626)
我只想说,使用C#中的Microsoft librairie,它可以使用相同的FTP服务器。
你知道为什么它在JAVA中不起作用吗?
谢谢:)
编辑:我终于找到了问题。 Windows的防火墙阻止Java访问FTP服务器,每次我的应用程序运行时,我都会使用此代码进行异常Process p = Runtime.getRuntime().exec("netsh advfirewall set global StatefulFTP disable");
p.waitFor();
BufferedReader reader = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line = reader.readLine();
while (line != null)
{
line = reader.readLine();
}