fix download files

This commit is contained in:
Mateusz Kowalczyk 2020-01-02 21:28:51 +01:00
parent da104fdd61
commit 89c6696c67
2 changed files with 24 additions and 19 deletions

View File

@ -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;

View File

@ -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) {