當我嘗試上傳文件到ftp服務器時,出現錯誤。該服務器是遠程服務器,但它提供了ftp訪問。這是我正在使用的代碼上傳java中的文件到ftp服務器時出錯
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package fileuploaddemo;
import org.apache.commons.net.ftp.FTPClient;
import java.io.FileInputStream;
import java.io.IOException;
public class FileUploadDemo {
public static void main(String[] args) {
FTPClient client = new FTPClient();
FileInputStream fis = null;
try {
client.connect("ftp.adress.comlu.com");
client.login("username", "mypass");
//
// Create an InputStream of the file to be uploaded
//
String filename = "D:/xxx/screen0.png";
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();
}
}
}
}
這是程序輸出。沒有上傳,我得到的只是這些行。
run:
java.net.SocketException: Software caused connection abort: recv failed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:150)
at java.net.SocketInputStream.read(SocketInputStream.java:121)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:154)
at java.io.BufferedReader.read(BufferedReader.java:175)
at org.apache.commons.net.io.CRLFLineReader.readLine(CRLFLineReader.java:58)
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:310)
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:290)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:479)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:552)
at org.apache.commons.net.ftp.FTP.port(FTP.java:877)
at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:709)
at org.apache.commons.net.ftp.FTPClient._storeFile(FTPClient.java:565)
at org.apache.commons.net.ftp.FTPClient.__storeFile(FTPClient.java:557)
at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:1795)
at fileuploaddemo.FileUploadDemo.main(FileUploadDemo.java:29)
BUILD SUCCESSFUL (total time: 1 second)
可能是什麼問題?
我試圖在服務器上存儲文件之前添加了行,現在它說java.net.SocketException:連接重置 – 2012-04-23 18:10:50
我相信它需要調用連接前調用,而不是。嘗試檢查javadoc http://commons.apache.org/net/api-3.1/org/apache/commons/net/ftp/FTPClient.html#enterLocalPassiveMode() – 2012-04-23 18:15:58
@Karuvägistaja你可以發佈新的例外嗎?你在防火牆後面嗎? – 2012-04-23 18:20:13