diff --git a/src/main/java/ftp/sar/builder/FilePaneBuilder.java b/src/main/java/ftp/sar/builder/FilePaneBuilder.java index bdff798..d9e7398 100644 --- a/src/main/java/ftp/sar/builder/FilePaneBuilder.java +++ b/src/main/java/ftp/sar/builder/FilePaneBuilder.java @@ -26,8 +26,8 @@ public class FilePaneBuilder { } private String getName(String fileName) { - if (fileName.length() > 120) { - fileName = fileName.substring(0, 120); + if (fileName.length() > 20) { + fileName = fileName.substring(0, 20); } return fileName; diff --git a/src/main/java/ftp/sar/server/ServerConnector.java b/src/main/java/ftp/sar/server/ServerConnector.java index 3814c51..a4983a0 100644 --- a/src/main/java/ftp/sar/server/ServerConnector.java +++ b/src/main/java/ftp/sar/server/ServerConnector.java @@ -5,6 +5,7 @@ import ftp.sar.exception.CannotConnectException; import java.io.*; import java.util.ArrayList; +import java.util.Base64; public class ServerConnector { private final Server server; @@ -79,39 +80,43 @@ public class ServerConnector { } public void getFile(String filename, String path) { - File file = new File(path + "\\" + filename); + File file = new File(path + "/" + filename); + DataInputStream dataInputStream = null; try { this.sendRequest("FTP_" + filename); } catch (CannotConnectException e) { e.printStackTrace(); } - DataInputStream dataInputStream = null; - char[] buffer = new char[512]; + byte[] buffer = new byte[512]; try { - dataInputStream = new DataInputStream(this.server.getSocket().getInputStream()); - InputStreamReader inputStreamReader = new InputStreamReader(dataInputStream); - BufferedReader reader = new BufferedReader(inputStreamReader); - reader.read(buffer); + this.server.getSocket().getInputStream().read(buffer, 0, 512); + + String lengthOfFile = new String(buffer, "UTF-8"); String l = ""; - for (char c: buffer) { + for (char c: lengthOfFile.toCharArray()) { if (Character.isDigit(c)) { l += c; + } else { + break; } } //TODO: Finish getting file -// long lengthOFFile = Long.parseLong(l.trim()); -// -// -// byte[] fileBuffer = new byte[4096]; -// -// do { -// dataInputStream.read(fileBuffer); -// lengthOFFile -= 4096; -// } while (lengthOFFile > 0); + long lengthOFFile = Long.parseLong(l.trim()); + + FileOutputStream fileOutputStream = new FileOutputStream(file); + + byte[] fileBuffer = new byte[4096]; + int part = 0; + do { + int bytesRead = this.server.getSocket().getInputStream().read(fileBuffer, 0, 4096); + lengthOFFile -= 4096; + part++; + fileOutputStream.write(fileBuffer, 0, bytesRead); + } while (lengthOFFile > 0); System.out.println(l); } catch (IOException e) {