/** * Convenience method, so that we don't open a new connection when using this method from within * another method. Otherwise every API invocation incurs the overhead of opening/closing a TCP * connection. * * @param client * @param src * @param dst * @return * @throws IOException */ private boolean rename(FTPClient client, Path src, Path dst) throws IOException { Path workDir = new Path(client.printWorkingDirectory()); Path absoluteSrc = makeAbsolute(workDir, src); Path absoluteDst = makeAbsolute(workDir, dst); if (!exists(client, absoluteSrc)) { throw new IOException("Source path " + src + " does not exist"); } if (exists(client, absoluteDst)) { throw new IOException("Destination path " + dst + " already exist, cannot rename!"); } String parentSrc = absoluteSrc.getParent().toUri().toString(); String parentDst = absoluteDst.getParent().toUri().toString(); String from = src.getName(); String to = dst.getName(); if (!parentSrc.equals(parentDst)) { throw new IOException( "Cannot rename parent(source): " + parentSrc + ", parent(destination): " + parentDst); } client.changeWorkingDirectory(parentSrc); boolean renamed = client.rename(from, to); return renamed; }
@Override public void sendInventoryToTiteLiveServer() { if (remoteFileName == null) { remoteFileName = "." + titeliveId + "_ART.asc"; } if (decimalFormat == null) { decimalFormat = new DecimalFormat("0.00"); decimalFormat.setDecimalSeparatorAlwaysShown(false); } FTPClient ftpClient = new FTPClient(); try (PipedInputStream inPipe = new PipedInputStream(PIPE_BUFFER)) { ftpClient.connect(ftpUrl); ftpClient.login(ftpUsername, ftpPassword); new Thread( () -> { try (PipedOutputStream outPipe = new PipedOutputStream(inPipe)) { IOUtils.write( "EXTRACTION STOCK DU " + new SimpleDateFormat("dd/MM/YYYY").format(new Date()) + "\r\n", outPipe); for (Integer productId : productInventoryService.findProductIdWithInventory()) { Product product = productService.findOne(productId); if (product.getEan13().length() == 13) { IOUtils.write( titeliveId + product.getEan13() + String.format( "%04d", new Double(product.getProductInventory().getQuantityOnHand()) .intValue()) + StringUtils.leftPad( decimalFormat .format(product.getPriceTaxIn()) .replace( String.valueOf( decimalFormat .getDecimalFormatSymbols() .getDecimalSeparator()), ""), 10, "0") + "\r\n", outPipe); } } } catch (IOException ioEx) { LOGGER.error("Error while writing content to FTP Server", ioEx); } }) .start(); ftpClient.storeFile(remoteFileName, inPipe); ftpClient.rename(remoteFileName, titeliveId + "_ART.asc"); LOGGER.info("Succesfully transfered inventory to Tite"); } catch (IOException ioEx) { throw new RuntimeException(ioEx); } finally { try { ftpClient.disconnect(); } catch (IOException ioEx) { LOGGER.error("Error while closing FTP connection.", ioEx); } } }