Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.Dimension;
- import java.awt.EventQueue;
- import java.awt.TextArea;
- import java.io.BufferedInputStream;
- import java.io.BufferedReader;
- import java.io.DataInputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.PrintWriter;
- import java.net.Socket;
- import java.nio.file.DirectoryNotEmptyException;
- import java.nio.file.Files;
- import java.nio.file.NoSuchFileException;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- import javax.swing.BoxLayout;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- public class Client {
- public static Scanner scanner;
- public static Socket socket;
- public static String fileName;
- public static String fileLocation;
- public static String ipAddress;
- public static String portNoS;
- public static int portNo;
- public static long size;
- public static int sizeR;
- public static File file;
- public static String response;
- public static String listString = "";
- public static ArrayList<String> list = new ArrayList<String>();
- public static OutputStream outputStream;
- public static ArchiveFrame window;
- public static void openSocket(String ipAddress, int portNo) throws IOException {
- try {
- socket = new Socket(ipAddress, portNo);
- System.out.println("connected.");
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static void closeSocket(String ipAddress, int portNo) throws IOException {
- try {
- } finally {
- if (socket != null)
- socket.close();
- }
- }
- public static void main(String[] args) throws IOException {
- ipAddress = JOptionPane.showInputDialog("Enter ipAddress of machine :");
- portNoS = JOptionPane.showInputDialog("Enter port number of machine(e.g. '5555') :");
- portNo = Integer.valueOf(portNoS);
- scanner = new Scanner(System.in);
- try {
- socket = new Socket(ipAddress, portNo);
- /*
- * scanner = new Scanner(System.in);
- *
- * } else if (command.equals("deletefile")) { sendDeleteRequest();
- * getResponse(); } else if (command.equals("zipfile")) {
- * sendZipFileRequest(); getResponse(); } else if
- * (command.equals("showarchive")) { sendShowArchiveRequest();
- * getResponse(); } else if (command.equals("returnfile")) {
- * sendReturnFileRequest(); BufferedReader br = new
- * BufferedReader(new InputStreamReader(socket.getInputStream()));
- * String request = br.readLine(); String[] parts =
- * request.split("-"); fileName = parts[0]; String sizeS = parts[1];
- * sizeR = Integer.valueOf(sizeS); receiveReturnedFile(fileLocation,
- * sizeR); getResponse(); } else if (command.equals("closeprogram"))
- * { Client.closeSocket(ipAddress, portNo); } else {
- * System.out.println("Option doesn't exist"); }
- */
- } catch (Exception e) {
- e.printStackTrace();
- }
- runFrame();
- }
- public static void runFrame() {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- window = new ArchiveFrame();
- window.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- public static void saveFileEvent(String filePath) throws IOException {
- // System.out.println("Please enter file location with file name (e.g.'D:\\abc.txt'): ");
- fileLocation = filePath;
- sendSaveRequest();
- getResponse();
- if (response.equals("notexist")) {
- send(ipAddress, portNo, fileLocation);
- getResponse();
- outputStream.flush();
- outputStream.close();
- delete();
- } else {
- window.textArea.append("File already exists on a server\n");
- }
- }
- // naprawic zeby jak plik nie istnieje - wywalal komende!!!!!!!!
- public static void deleteFileEvent(String fileNameD) throws IOException {
- System.out.println("Please enter file location with file name (e.g.'D:\\abc.txt'): ");
- if(response.equals("deletefile")){
- sendDeleteRequest(fileNameD);
- getResponse();
- }
- else{
- window.textArea.append("No such file in the archive\n");
- }
- }
- public static void sendZipFileEvent(String filePath) throws IOException {
- if (response.equals("zipFile")) {
- sendZipFileRequest();
- getResponse();
- } else {
- window.textArea.append("...\n");
- }
- }
- private static void getResponse() throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- int count = 1;
- // String response;
- while ((response = br.readLine()) != null) {
- window.textArea.append(response);
- // String text=response;
- // JOptionPane.showMessageDialog(null, text);
- break;
- }
- }
- private static void sendSaveRequest() throws IOException {
- File file = new File(fileLocation);
- fileName = file.getName();
- size = file.length();
- String sizeS = String.valueOf(size);
- //System.out.println("Sending save request.");
- window.textArea.append("Sending save request.\n");
- window.textArea.append("name:" + fileName + "\n");
- window.textArea.append("size:" + sizeS + "\n");
- OutputStream os = socket.getOutputStream();
- PrintWriter pw = new PrintWriter(os, true);
- pw.println("savefile-" + fileName + "-" + sizeS);
- pw.flush();
- }
- private static void sendDeleteRequest(String fileName) throws IOException {
- String fileNameD = fileName;
- // scanner = new Scanner(System.in);
- // System.out.println("Please enter file to delete (e.g.'abc.txt'): ");
- // fileNameD = scanner.next();
- int sizeS = 0;
- // System.out.println("Sending delete request.");
- // System.out.println("name:" + fileNameD);
- String text = ("Sending delete request for:" + fileNameD);
- JOptionPane.showMessageDialog(null, text);
- OutputStream os = socket.getOutputStream();
- PrintWriter pw = new PrintWriter(os, true);
- pw.println("deletefile-" + fileNameD + "-" + sizeS);
- pw.flush();
- }
- private static void sendZipFileRequest() throws IOException {
- String fileNameD;
- // scanner = new Scanner(System.in);
- // System.out.println("Please enter file from server to zip
- // (e.g.'abc.txt'): ");
- // fileNameD = scanner.next();
- fileNameD = JOptionPane.showInputDialog("Please enter file to zip (e.g.'abc.txt'): ");
- int sizeS = 0;
- // System.out.println("Sending zip request.");
- // System.out.println("name:" + fileNameD);
- String text = ("Sending zip request for:" + fileNameD);
- JOptionPane.showMessageDialog(null, text);
- OutputStream os = socket.getOutputStream();
- PrintWriter pw = new PrintWriter(os, true);
- pw.println("zipfile-" + fileNameD + "-" + sizeS);
- pw.flush();
- }
- private static void sendShowArchiveRequest() throws IOException {
- String fileNameD = null;
- int sizeS = 0;
- // System.out.println("Sending showarchive request.");
- String text = ("Sending showarchive request.");
- JOptionPane.showMessageDialog(null, text);
- OutputStream os = socket.getOutputStream();
- PrintWriter pw = new PrintWriter(os, true);
- pw.println("showarchive-" + fileNameD + "-" + sizeS);
- pw.flush();
- }
- private static void sendReturnFileRequest() throws IOException {
- String fileNameD;
- // scanner = new Scanner(System.in);
- // System.out.println("Please enter file to return (e.g.'abc.txt'): ");
- // fileNameD = scanner.next();
- fileNameD = JOptionPane.showInputDialog("Please enter file to get from archive (e.g.'abc.txt'): ");
- int sizeS = 0;
- String text = ("Sending return file request for:" + fileNameD);
- JOptionPane.showMessageDialog(null, text);
- // System.out.println("Sending returnfile request.");
- // System.out.println("name:" + fileNameD);
- OutputStream os = socket.getOutputStream();
- PrintWriter pw = new PrintWriter(os, true);
- pw.println("returnfile-" + fileNameD + "-" + sizeS);
- pw.flush();
- }
- public static void send(String ipAddress, int portNo, String fileLocation) throws IOException {
- FileInputStream fileInputStream = null;
- BufferedInputStream bufferedInputStream = null;
- // OutputStream outputStream = null;
- try {
- window.textArea.append("Waiting for receiver...\n");
- try {
- int partCounter = 1;
- int sizeOfFiles = 1024 * 1024;// 1MB
- byte[] buffer = new byte[sizeOfFiles];
- File file = new File(fileLocation);
- fileName = file.getName();
- int count = 0;
- try (FileInputStream fis = new FileInputStream(file);
- BufferedInputStream bis = new BufferedInputStream(fis)) {
- int bytesAmount = 0;
- // List<File> listOfFilesD = new ArrayList<File>();
- while ((bytesAmount = bis.read(buffer)) > 0) {
- // write each chunk of data into separate file with
- // different number in name
- String filePartName = String.format(fileName + "." + partCounter);
- File newFile = new File(file.getParent(), filePartName);
- FileOutputStream out = new FileOutputStream(newFile);
- try {
- out.write(buffer, 0, bytesAmount);
- count++;
- // listOfFilesD.add(newFile);
- window.textArea.append("File added" + filePartName + "\n");
- list.add("" + filePartName);
- out.close();
- } catch (Exception e) {
- System.out.println(e);
- }
- byte[] byteArray = new byte[(int) newFile.length()];
- fileInputStream = new FileInputStream(newFile);
- bufferedInputStream = new BufferedInputStream(fileInputStream);
- bufferedInputStream.read(byteArray, 0, byteArray.length); // copied
- // file
- // into
- // byteArray
- // sending file through socket
- outputStream = socket.getOutputStream();
- window.textArea.append("Sending " + fileLocation + "( size: " + byteArray.length + " bytes)\n");
- outputStream.write(byteArray, 0, byteArray.length); // copying
- // byteArray
- // to
- // socket
- outputStream.flush(); // flushing socket
- window.textArea.append("Done.\n"); // file has been sent
- partCounter++;
- }
- }
- } finally {
- if (bufferedInputStream != null)
- bufferedInputStream.close();
- if (outputStream != null)
- bufferedInputStream.close();
- // if (socket != null)
- // socket.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- // if (serverSocket != null)
- // serverSocket.close();
- }
- }
- public static void delete() throws IOException {
- for (String s : list) {
- listString += s + "\t";
- }
- window.textArea.append(listString + "\n");
- String fname;
- Path p = Paths.get(fileLocation);
- Path folder = p.getParent();
- // String textPath=(fileLocation+fname);
- // Path path = Paths.get(textPath);
- for (int i = 0; i < list.size(); i++) {
- fname = list.get(i);
- window.textArea.append(fname);
- // String[] parts = fname.split(".");
- // String nu = parts[2];
- String textPath = (folder + "\\" + fname);
- Path path = Paths.get(textPath);
- try {
- Files.delete(path);
- } catch (NoSuchFileException x) {
- System.err.format("%s: no such" + " file or directory%n", path);
- } catch (DirectoryNotEmptyException x) {
- System.err.format("%s not empty%n", path);
- } catch (IOException x) {
- // File permission problems are caught here.
- System.err.println(x);
- }
- }
- }
- public static void receiveReturnedFile(String fileLocation, int sizeR) throws IOException {
- window.textArea.append("Saving file...");
- fileLocation = ("C:\\Users\\Dominika\\Desktop\\JAVAR\\" + fileName);
- DataInputStream dis = new DataInputStream(socket.getInputStream());
- FileOutputStream fos = new FileOutputStream(fileLocation);
- byte[] buffer = new byte[sizeR];
- window.textArea.append("rozmiar" + sizeR);
- window.textArea.append("Nazwa" + fileLocation);
- int read = 0;
- int totalRead = 0;
- int remaining = sizeR;
- try {
- while ((read = dis.read(buffer, 0, Math.min(buffer.length, remaining))) > 0) {
- totalRead += read;
- remaining -= read;
- fos.write(buffer, 0, read);
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- fos.close();
- // dis.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement