Hey Leute,
[CODE lang="java" title="Send"]JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File file = new File(chooser.getSelectedFile().getAbsolutePath());
byte[] fileContent = null;
try {
fileContent = Files.readAllBytes(file.toPath());
int lenght = fileContent.length;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int currentByte = 0;
while(lenght > 4096) {
bos.write(fileContent, currentByte, 4096);
FileM m = new FileM(bos.toByteArray(), true);
ClientHandler.sendFile(m);
currentByte += 4096;
lenght -= 4096;
bos.reset();
System.out.println(lenght);
}
if(lenght > 0) {
bos.write(fileContent, currentByte, lenght);
FileM m = new FileM(bos.toByteArray(), true);
ClientHandler.sendFile(m);
}
FileM m = new FileM(file.getName(), false);
ClientHandler.sendFile(m);
bos.reset();
bos.close();
fileContent = null;
} catch (IOException e1) {
e1.printStackTrace();
}[/CODE]
[CODE lang="java" title="FileM"]public class FileM {
byte[] bytes;
String FileName;
boolean isWriting;
public FileM() {}
public FileM(byte[] bytes,boolean isWriting) {
this.isWriting = isWriting;
this.bytes = bytes;
}public FileM(String FileName,boolean isWriting) {
this.isWriting = isWriting;
this.FileName = FileName;
}
public boolean isWriting() {
return isWriting;
}
public void setWriting(boolean isWriting) {
this.isWriting = isWriting;
}
public String getEndung() {
return FileName;
}
public void setEndung(String FileName) {
this.FileName = FileName;
}
public byte[] getBytes() {
return bytes;
}
}[/CODE]
[CODE lang="java" title="received"]if(o instanceof FileM) {
FileM f = (FileM)o;
if(f.isWriting()) {
try {
FileOutputStream out = new FileOutputStream("C:\\Users\\"+user+"\\AppData\\Local\\Temp\\SendingFile.txt",true);
out.write(f.getBytes());
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else if(!f.isWriting()) {
String name = f.getEndung();
File file = new File("C:\\Users\\"+user+"\\AppData\\Local\\Temp\\SendingFile.txt");
int i = JOptionPane.showOptionDialog(null, "Möchtest du die datei "+ name +"\nGröße:"+file.length() / 1024+"kb", "File", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE,null,null, null);
if(i == JOptionPane.YES_OPTION) {
file.renameTo(new File("C:\\Users\\"+user+"\\Downloads\\"+name));
}else if(i == JOptionPane.NO_OPTION) {
file.delete();
}
}
}[/CODE]
[CODE lang="java" title="exeption"]Exception in thread "AWT-EventQueue-0" com.esotericsoftware.kryo.KryoException: Buffer overflow. Available: 0, required: 35[/CODE]
Das Programm funktioniert so weit die Datei wird geschickt aber, wenn die Datei eine bestimme Größe überschreite kommt eine Fehlermeldung. Das Programm verbraucht auch gute 4 GB RAM.
Liebe Grüße.
[CODE lang="java" title="Send"]JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File file = new File(chooser.getSelectedFile().getAbsolutePath());
byte[] fileContent = null;
try {
fileContent = Files.readAllBytes(file.toPath());
int lenght = fileContent.length;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int currentByte = 0;
while(lenght > 4096) {
bos.write(fileContent, currentByte, 4096);
FileM m = new FileM(bos.toByteArray(), true);
ClientHandler.sendFile(m);
currentByte += 4096;
lenght -= 4096;
bos.reset();
System.out.println(lenght);
}
if(lenght > 0) {
bos.write(fileContent, currentByte, lenght);
FileM m = new FileM(bos.toByteArray(), true);
ClientHandler.sendFile(m);
}
FileM m = new FileM(file.getName(), false);
ClientHandler.sendFile(m);
bos.reset();
bos.close();
fileContent = null;
} catch (IOException e1) {
e1.printStackTrace();
}[/CODE]
[CODE lang="java" title="FileM"]public class FileM {
byte[] bytes;
String FileName;
boolean isWriting;
public FileM() {}
public FileM(byte[] bytes,boolean isWriting) {
this.isWriting = isWriting;
this.bytes = bytes;
}public FileM(String FileName,boolean isWriting) {
this.isWriting = isWriting;
this.FileName = FileName;
}
public boolean isWriting() {
return isWriting;
}
public void setWriting(boolean isWriting) {
this.isWriting = isWriting;
}
public String getEndung() {
return FileName;
}
public void setEndung(String FileName) {
this.FileName = FileName;
}
public byte[] getBytes() {
return bytes;
}
}[/CODE]
[CODE lang="java" title="received"]if(o instanceof FileM) {
FileM f = (FileM)o;
if(f.isWriting()) {
try {
FileOutputStream out = new FileOutputStream("C:\\Users\\"+user+"\\AppData\\Local\\Temp\\SendingFile.txt",true);
out.write(f.getBytes());
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else if(!f.isWriting()) {
String name = f.getEndung();
File file = new File("C:\\Users\\"+user+"\\AppData\\Local\\Temp\\SendingFile.txt");
int i = JOptionPane.showOptionDialog(null, "Möchtest du die datei "+ name +"\nGröße:"+file.length() / 1024+"kb", "File", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE,null,null, null);
if(i == JOptionPane.YES_OPTION) {
file.renameTo(new File("C:\\Users\\"+user+"\\Downloads\\"+name));
}else if(i == JOptionPane.NO_OPTION) {
file.delete();
}
}
}[/CODE]
[CODE lang="java" title="exeption"]Exception in thread "AWT-EventQueue-0" com.esotericsoftware.kryo.KryoException: Buffer overflow. Available: 0, required: 35[/CODE]
Das Programm funktioniert so weit die Datei wird geschickt aber, wenn die Datei eine bestimme Größe überschreite kommt eine Fehlermeldung. Das Programm verbraucht auch gute 4 GB RAM.
Liebe Grüße.