RT。我现在在xp虚拟机上用Serv-U建立了一个FTP服务器做测试。程序端用apache.commons.net.ftp这个进行FTP设置。
开始用的是FTP进行上传图片。正常
后来参照google查找的例子,将FTP改为FTPS。再进行FTPS的上传图片时候。可以在服务器上产生一个图片。但却一直是0字节。
并且在out.write(bytes, 0, c)输出内容的时候抛Connection reset by peer: socket write error这个错误。
因为刚接触FTPS这里。所以有些迷惑。是不是服务器端需要进行设置?为什么能生成图片却是0字节?报socket错误是超时引起的吗?谢谢浏览的各位朋友!!
String protocol = "SSL"; // TLS / SSL
boolean isImpicit = true;
public FTPSClient ftpClient = new FTPSClient(protocol, isImpicit);
//public FTPClient ftpClient = new FTPClient();//如果换成FTP就正确
OutputStream out = ftpClient.appendFileStream(remoteFile);
// 断点续传
if (remoteSize > 0) {
ftpClient.setRestartOffset(remoteSize);
process = remoteSize / step;
raf.seek(remoteSize);
localreadbytes = remoteSize;
}
byte[] bytes = new byte[1024];
int c;
while ((c = raf.read(bytes)) != -1) {
out.write(bytes, 0, c);//FTPS的时候。这里一直抛异常java.net.SocketException: Connection reset by peer: socket write error
localreadbytes += c;
if (localreadbytes / step != process) {
process = localreadbytes / step;
logger.debug("上传进度:" + process);
}
}
out.flush();
raf.close();
out.close();
开始用的是FTP进行上传图片。正常
后来参照google查找的例子,将FTP改为FTPS。再进行FTPS的上传图片时候。可以在服务器上产生一个图片。但却一直是0字节。
并且在out.write(bytes, 0, c)输出内容的时候抛Connection reset by peer: socket write error这个错误。
因为刚接触FTPS这里。所以有些迷惑。是不是服务器端需要进行设置?为什么能生成图片却是0字节?报socket错误是超时引起的吗?谢谢浏览的各位朋友!!
String protocol = "SSL"; // TLS / SSL
boolean isImpicit = true;
public FTPSClient ftpClient = new FTPSClient(protocol, isImpicit);
//public FTPClient ftpClient = new FTPClient();//如果换成FTP就正确
OutputStream out = ftpClient.appendFileStream(remoteFile);
// 断点续传
if (remoteSize > 0) {
ftpClient.setRestartOffset(remoteSize);
process = remoteSize / step;
raf.seek(remoteSize);
localreadbytes = remoteSize;
}
byte[] bytes = new byte[1024];
int c;
while ((c = raf.read(bytes)) != -1) {
out.write(bytes, 0, c);//FTPS的时候。这里一直抛异常java.net.SocketException: Connection reset by peer: socket write error
localreadbytes += c;
if (localreadbytes / step != process) {
process = localreadbytes / step;
logger.debug("上传进度:" + process);
}
}
out.flush();
raf.close();
out.close();
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.security.NoSuchAlgorithmException;import javax.net.SocketFactory;
import javax.net.ssl.SSLSocketFactory;import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPConnectionClosedException;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTPSClient;public final class FTPSExample {
public static final String USAGE = "Usage: ftp [-s] [-b] <127.0.0.1> <shanming> <shanming> <test.txt> <shanming.txt>\n"
+ "\nDefault behavior is to download a file and use ASCII transfer mode.\n"
+ "\t-s store file on server (upload)\n"
+ "\t-b use binary transfer mode\n"; public static final void main(String[] args)
throws NoSuchAlgorithmException {
int base = 0;
boolean storeFile = false, binaryTransfer = false, error = false;
String server, username, password, remote, local;
String protocol = "SSL"; // SSL/TLS
FTPSClient ftps;
for (base = 0; base < args.length; base++) {
if (args[base].startsWith("-s"))
storeFile = true;
else if (args[base].startsWith("-b"))
binaryTransfer = true;
else
break;
}
if ((args.length - base) != 5) {
System.err.println(USAGE);
System.exit(1);
}
server = args[base++];
username = args[base++];
password = args[base++];
remote = args[base++];
local = args[base];
ftps = new FTPSClient(protocol);
//ftps.enterLocalPassiveMode();
ftps.addProtocolCommandListener(new PrintCommandListener(
new PrintWriter(System.out)));
try {
int reply;
ftps.connect(server);
System.out.println("Connected to " + server + ".");
// After connection attempt, you should check the reply code to
// verify
// success.
reply = ftps.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftps.disconnect();
System.err.println("FTP server refused connection.");
System.exit(1);
}
} catch (IOException e) {
if (ftps.isConnected()) {
try {
ftps.disconnect();
} catch (IOException f) {
// do nothing
}
}
System.err.println("Could not connect to server.");
e.printStackTrace();
System.exit(1);
}
__main: try {
ftps.setBufferSize(1000);
if (!ftps.login(username, password, "990")) {
ftps.logout();
error = true;
break __main;
}
ftps.execPROT("P");
System.out.println("Remote system is: " + ftps.getSystemName());
System.out.println("Working directory is: "
+ ftps.printWorkingDirectory());
System.out.println("default port is: " + ftps.getDefaultPort());
// if (binaryTransfer)
// ftps.setFileType(FTP.BINARY_FILE_TYPE);
// Use passive mode as default because most of us are
// behind firewalls these days.
//SocketFactory factory = SSLSocketFactory.getDefault();
//ftps.setSocketFactory(factory);
FTPFile[] files = ftps.listFiles();
for (FTPFile file : files) {
System.out.println("ftpfile name is *****************:" + file.getName());
} if (storeFile) {
System.out.println("Here is upload!");
ftps.changeWorkingDirectory("/");
InputStream input;
input = new FileInputStream(local);
ftps.storeFile(remote, input);
input.close();
} else {
System.out.println("Here is download!");
ftps.changeWorkingDirectory("/");
OutputStream output;
output = new FileOutputStream(local);
ftps.retrieveFile(remote, output);
output.close();
}
ftps.logout();
} catch (FTPConnectionClosedException e) {
error = true;
System.err.println("Server closed connection.");
e.printStackTrace();
} catch (IOException e) {
error = true;
e.printStackTrace();
} finally {
if (ftps.isConnected()) {
try {
ftps.disconnect();
} catch (IOException f) {
// do nothing
}
}
}
System.exit(error ? 1 : 0);
} // end main
}