i have a serious (and weird) problem when uploading files to a FTPS server.
I have to upload JSON files and some of them are really small (under 5kB). There is no problem with these files, I can upload them successfully.
But when uploading "larger" files (not really large, over 30kb) it shows this error:
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
Here's my uploading code
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
}
}
}
I've already tried with outpustream and I got the same error. Also tried uploading manually with WinSCP and it worked, so it's not a permissions issue.
I managed to solve this. What happened is that the FTPS server was using an SSL v3 protocol for the connections, and this protocol is deprecated by java because of some security breaches.
The best solution is to change the protocol for t.he FTPs server