FTPSClient 仅在上传 diminute 文件时成功
FTPSClient only succeeds when uploading diminute files
我在将文件上传到 FTPS 服务器时遇到了一个严重(而且很奇怪)的问题。
我必须上传 JSON 个文件,其中一些文件非常小(不到 5kB)。这些文件没有问题,我可以上传成功
但是在上传 "larger" 个文件(不是很大,超过 30kb)时显示此错误:
java.net.SocketException: Connection reset by peer: 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 java.io.BufferedOutputStream.flushBuffer(Unknown Source)
at java.io.BufferedOutputStream.flush(Unknown Source)
at java.io.FilterOutputStream.flush(Unknown Source)
at java.io.FilterOutputStream.close(Unknown Source)
at org.apache.commons.net.ftp.FTPClient._storeFile(FTPClient.java:688)
at org.apache.commons.net.ftp.FTPClient.__storeFile(FTPClient.java:639)
at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:2030)
at com.spsa.integracion.gastosingresos.business.impl.GastosIngresosIFImpl.envioArchivoAFTPS(GastosIngresosIFImpl.java:206)
at com.spsa.integracion.gastosingresos.business.impl.GastosIngresosIFImpl.ejecutar(GastosIngresosIFImpl.java:90)
at com.spsa.integracion.gastosingresos.GastosIngresosApplication.run(GastosIngresosApplication.java:27)
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:800)
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:784)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:338)
at com.spsa.integracion.gastosingresos.GastosIngresosApplication.main(GastosIngresosApplication.java:20)
Suppressed: 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 java.io.BufferedOutputStream.flushBuffer(Unknown Source)
at java.io.BufferedOutputStream.flush(Unknown Source)
at java.io.FilterOutputStream.close(Unknown Source)
... 11 more
这是我的上传代码
boolean binaryTransfer = false, success = false;
FTPSClient ftps;
fileName = "A134_GASTOS_M_20181024_184808.JSON";
int port = Integer.parseInt(applicationProperties.getFtpsPort());
String server = applicationProperties.getFtpsHost();
String username = applicationProperties.getFtpsUsername();
String password = applicationProperties.getFtpsPassword();
String remote = "\" + applicationProperties.getFolder() + "\" + fileName;
String local = "\" + fileName;
String errorMessage = "";
ftps = new FTPSClient(true);
ftps.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
try {
int reply;
ftps.connect(server, port);
System.out.println("Conectado a " + server + ".");
reply = ftps.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftps.disconnect();
System.err.println("El servidor FTP rechazo la conexion");
System.exit(1);
}
} catch (IOException e) {
if (ftps.isConnected()) {
try {
ftps.disconnect();
} catch (IOException f) {
}
}
System.err.println("No se pudo conectar al servidor");
e.printStackTrace();
errorMessage = "No se pudo conectar al servidor";
System.exit(1);
}
try {
ftps.setBufferSize(1000);
if (!ftps.login(username, password)) {
ftps.logout();
}
System.out.println("Remote system is " + ftps.getSystemName());
if (binaryTransfer)
ftps.setFileType(FTP.BINARY_FILE_TYPE);
// Use passive mode as default because most of us are
// behind firewalls these days.
ftps.enterLocalPassiveMode();
InputStream input;
input = new FileInputStream(local);
ftps.storeFile(remote, input);
input.close();
ftps.logout();
success = true;
} catch (FTPConnectionClosedException e) {
System.err.println("Conexion FTP cerrada");
errorMessage = "Conexion FTP cerrada";
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
errorMessage = "Conexion FTP cerrada";
} finally {
if (ftps.isConnected()) {
try {
ftps.disconnect();
} catch (IOException f) {
// do nothing
}
}
}
我已经尝试过使用 outpustream,但我遇到了同样的错误。也尝试过使用 WinSCP 手动上传并且成功了,所以这不是权限问题。
我设法解决了这个问题。发生的事情是 FTPS 服务器使用 SSL v3 协议进行连接,由于存在一些安全漏洞,该协议已被 java 弃用。
最佳解决方案是更改 t.he FTP 服务器
的协议
我在将文件上传到 FTPS 服务器时遇到了一个严重(而且很奇怪)的问题。
我必须上传 JSON 个文件,其中一些文件非常小(不到 5kB)。这些文件没有问题,我可以上传成功
但是在上传 "larger" 个文件(不是很大,超过 30kb)时显示此错误:
java.net.SocketException: Connection reset by peer: 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 java.io.BufferedOutputStream.flushBuffer(Unknown Source)
at java.io.BufferedOutputStream.flush(Unknown Source)
at java.io.FilterOutputStream.flush(Unknown Source)
at java.io.FilterOutputStream.close(Unknown Source)
at org.apache.commons.net.ftp.FTPClient._storeFile(FTPClient.java:688)
at org.apache.commons.net.ftp.FTPClient.__storeFile(FTPClient.java:639)
at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:2030)
at com.spsa.integracion.gastosingresos.business.impl.GastosIngresosIFImpl.envioArchivoAFTPS(GastosIngresosIFImpl.java:206)
at com.spsa.integracion.gastosingresos.business.impl.GastosIngresosIFImpl.ejecutar(GastosIngresosIFImpl.java:90)
at com.spsa.integracion.gastosingresos.GastosIngresosApplication.run(GastosIngresosApplication.java:27)
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:800)
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:784)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:338)
at com.spsa.integracion.gastosingresos.GastosIngresosApplication.main(GastosIngresosApplication.java:20)
Suppressed: 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 java.io.BufferedOutputStream.flushBuffer(Unknown Source)
at java.io.BufferedOutputStream.flush(Unknown Source)
at java.io.FilterOutputStream.close(Unknown Source)
... 11 more
这是我的上传代码
boolean binaryTransfer = false, success = false;
FTPSClient ftps;
fileName = "A134_GASTOS_M_20181024_184808.JSON";
int port = Integer.parseInt(applicationProperties.getFtpsPort());
String server = applicationProperties.getFtpsHost();
String username = applicationProperties.getFtpsUsername();
String password = applicationProperties.getFtpsPassword();
String remote = "\" + applicationProperties.getFolder() + "\" + fileName;
String local = "\" + fileName;
String errorMessage = "";
ftps = new FTPSClient(true);
ftps.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
try {
int reply;
ftps.connect(server, port);
System.out.println("Conectado a " + server + ".");
reply = ftps.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftps.disconnect();
System.err.println("El servidor FTP rechazo la conexion");
System.exit(1);
}
} catch (IOException e) {
if (ftps.isConnected()) {
try {
ftps.disconnect();
} catch (IOException f) {
}
}
System.err.println("No se pudo conectar al servidor");
e.printStackTrace();
errorMessage = "No se pudo conectar al servidor";
System.exit(1);
}
try {
ftps.setBufferSize(1000);
if (!ftps.login(username, password)) {
ftps.logout();
}
System.out.println("Remote system is " + ftps.getSystemName());
if (binaryTransfer)
ftps.setFileType(FTP.BINARY_FILE_TYPE);
// Use passive mode as default because most of us are
// behind firewalls these days.
ftps.enterLocalPassiveMode();
InputStream input;
input = new FileInputStream(local);
ftps.storeFile(remote, input);
input.close();
ftps.logout();
success = true;
} catch (FTPConnectionClosedException e) {
System.err.println("Conexion FTP cerrada");
errorMessage = "Conexion FTP cerrada";
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
errorMessage = "Conexion FTP cerrada";
} finally {
if (ftps.isConnected()) {
try {
ftps.disconnect();
} catch (IOException f) {
// do nothing
}
}
}
我已经尝试过使用 outpustream,但我遇到了同样的错误。也尝试过使用 WinSCP 手动上传并且成功了,所以这不是权限问题。
我设法解决了这个问题。发生的事情是 FTPS 服务器使用 SSL v3 协议进行连接,由于存在一些安全漏洞,该协议已被 java 弃用。
最佳解决方案是更改 t.he FTP 服务器
的协议