public void sendFile(final File f) {
Runnable r = new Runnable() {
public void run() {
System.out.println("Sending " + f.getName() + "...");
out.println("@FILE_SEND");
out.println(f.getName());
try {
byte[] byteArray = new byte[1024];
FileInputStream fis = new FileInputStream(f.getPath());
long s;
s = f.length();
out.println(s);
int sp = (int)(s / 1024);
if (s % 1024 != 0) sp++;
Main.mainFrame.createProgressPanel(f.getPath(), sp);
BufferedOutputStream bos = new BufferedOutputStream(clientSocket.getOutputStream());
Thread.sleep(500);
while (s > 0) {
int i = fis.read(byteArray);
bos.write(byteArray, 0, i);
Main.mainFrame.progressInStream(1);
s-= i;
}
bos.flush();
fis.close();
} catch (FileNotFoundException e) {
System.err.println("File not found!");
} catch (IOException e) {
System.err.println("IOException");
} catch (Exception e) {
}
new JOptionPane().showMessageDialog(null, f.getName() + " Sent");
if (type == ClientServer.CLIENT_TYPE) Main.mainFrame.refreshFileList();
}
};
new Thread(r).start();
}
private void recieveFile(String filename) {
try {
long s;
s = Long.parseLong(in.readLine());
System.out.println("File size: " + s);
byte[] byteArray = new byte[1024];
new File("Recieved").mkdir();
File f = new File("./Recieved/" + filename);
f.createNewFile();
FileOutputStream fos = new FileOutputStream(f);
int sp = (int)(s / 1024);
if (s % 1024 != 0) sp++;
Main.mainFrame.createProgressPanel(filename, sp);
BufferedInputStream bis = new BufferedInputStream(clientSocket.getInputStream());
while (s > 0) {
int i = bis.read(byteArray);
fos.write(byteArray, 0, i);
Main.mainFrame.progressInStream(1);
s-= i;
}
fos.close();
} catch (IOException e) {
System.err.println("Recieve IO Error");
}
new JOptionPane().showMessageDialog(null, "Recieved " + filename);
}