Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.21 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.Dimension;
  3. import java.awt.EventQueue;
  4. import java.awt.TextArea;
  5. import java.io.BufferedInputStream;
  6. import java.io.BufferedReader;
  7. import java.io.DataInputStream;
  8. import java.io.File;
  9. import java.io.FileInputStream;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12. import java.io.InputStreamReader;
  13. import java.io.OutputStream;
  14. import java.io.PrintWriter;
  15. import java.net.Socket;
  16. import java.nio.file.DirectoryNotEmptyException;
  17. import java.nio.file.Files;
  18. import java.nio.file.NoSuchFileException;
  19. import java.nio.file.Path;
  20. import java.nio.file.Paths;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import java.util.Scanner;
  24.  
  25. import javax.swing.BoxLayout;
  26. import javax.swing.JButton;
  27. import javax.swing.JFrame;
  28. import javax.swing.JOptionPane;
  29.  
  30. public class Client {
  31. public static Scanner scanner;
  32. public static Socket socket;
  33. public static String fileName;
  34. public static String fileLocation;
  35. public static String ipAddress;
  36. public static String portNoS;
  37. public static int portNo;
  38. public static long size;
  39. public static int sizeR;
  40. public static File file;
  41. public static String response;
  42. public static String listString = "";
  43. public static ArrayList<String> list = new ArrayList<String>();
  44. public static OutputStream outputStream;
  45. public static ArchiveFrame window;
  46.  
  47. public static void openSocket(String ipAddress, int portNo) throws IOException {
  48. try {
  49. socket = new Socket(ipAddress, portNo);
  50. System.out.println("connected.");
  51.  
  52. } catch (IOException e) {
  53. e.printStackTrace();
  54. }
  55. }
  56.  
  57. public static void closeSocket(String ipAddress, int portNo) throws IOException {
  58. try {
  59. } finally {
  60. if (socket != null)
  61. socket.close();
  62. }
  63. }
  64.  
  65. public static void main(String[] args) throws IOException {
  66. ipAddress = JOptionPane.showInputDialog("Enter ipAddress of machine :");
  67. portNoS = JOptionPane.showInputDialog("Enter port number of machine(e.g. '5555') :");
  68. portNo = Integer.valueOf(portNoS);
  69. scanner = new Scanner(System.in);
  70.  
  71. try {
  72. socket = new Socket(ipAddress, portNo);
  73.  
  74. /*
  75. * scanner = new Scanner(System.in);
  76. *
  77. * } else if (command.equals("deletefile")) { sendDeleteRequest();
  78. * getResponse(); } else if (command.equals("zipfile")) {
  79. * sendZipFileRequest(); getResponse(); } else if
  80. * (command.equals("showarchive")) { sendShowArchiveRequest();
  81. * getResponse(); } else if (command.equals("returnfile")) {
  82. * sendReturnFileRequest(); BufferedReader br = new
  83. * BufferedReader(new InputStreamReader(socket.getInputStream()));
  84. * String request = br.readLine(); String[] parts =
  85. * request.split("-"); fileName = parts[0]; String sizeS = parts[1];
  86. * sizeR = Integer.valueOf(sizeS); receiveReturnedFile(fileLocation,
  87. * sizeR); getResponse(); } else if (command.equals("closeprogram"))
  88. * { Client.closeSocket(ipAddress, portNo); } else {
  89. * System.out.println("Option doesn't exist"); }
  90. */
  91. } catch (Exception e) {
  92. e.printStackTrace();
  93. }
  94. runFrame();
  95.  
  96. }
  97.  
  98. public static void runFrame() {
  99. EventQueue.invokeLater(new Runnable() {
  100. public void run() {
  101. try {
  102. window = new ArchiveFrame();
  103. window.setVisible(true);
  104. } catch (Exception e) {
  105. e.printStackTrace();
  106. }
  107. }
  108. });
  109. }
  110. public static void saveFileEvent(String filePath) throws IOException {
  111. // System.out.println("Please enter file location with file name (e.g.'D:\\abc.txt'): ");
  112. fileLocation = filePath;
  113. sendSaveRequest();
  114. getResponse();
  115. if (response.equals("notexist")) {
  116. send(ipAddress, portNo, fileLocation);
  117. getResponse();
  118. outputStream.flush();
  119. outputStream.close();
  120. delete();
  121. } else {
  122. window.textArea.append("File already exists on a server\n");
  123.  
  124. }
  125. }
  126.  
  127. // naprawic zeby jak plik nie istnieje - wywalal komende!!!!!!!!
  128.  
  129. public static void deleteFileEvent(String fileNameD) throws IOException {
  130. System.out.println("Please enter file location with file name (e.g.'D:\\abc.txt'): ");
  131. if(response.equals("deletefile")){
  132. sendDeleteRequest(fileNameD);
  133. getResponse();
  134. }
  135. else{
  136. window.textArea.append("No such file in the archive\n");
  137. }
  138. }
  139.  
  140. public static void sendZipFileEvent(String filePath) throws IOException {
  141.  
  142. if (response.equals("zipFile")) {
  143. sendZipFileRequest();
  144. getResponse();
  145. } else {
  146. window.textArea.append("...\n");
  147.  
  148. }
  149. }
  150.  
  151. private static void getResponse() throws IOException {
  152. BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  153. int count = 1;
  154. // String response;
  155.  
  156. while ((response = br.readLine()) != null) {
  157. window.textArea.append(response);
  158. // String text=response;
  159. // JOptionPane.showMessageDialog(null, text);
  160. break;
  161. }
  162. }
  163. private static void sendSaveRequest() throws IOException {
  164. File file = new File(fileLocation);
  165. fileName = file.getName();
  166. size = file.length();
  167. String sizeS = String.valueOf(size);
  168.  
  169. //System.out.println("Sending save request.");
  170. window.textArea.append("Sending save request.\n");
  171. window.textArea.append("name:" + fileName + "\n");
  172. window.textArea.append("size:" + sizeS + "\n");
  173.  
  174. OutputStream os = socket.getOutputStream();
  175. PrintWriter pw = new PrintWriter(os, true);
  176. pw.println("savefile-" + fileName + "-" + sizeS);
  177. pw.flush();
  178. }
  179.  
  180. private static void sendDeleteRequest(String fileName) throws IOException {
  181.  
  182. String fileNameD = fileName;
  183. // scanner = new Scanner(System.in);
  184. // System.out.println("Please enter file to delete (e.g.'abc.txt'): ");
  185. // fileNameD = scanner.next();
  186. int sizeS = 0;
  187.  
  188. // System.out.println("Sending delete request.");
  189. // System.out.println("name:" + fileNameD);
  190. String text = ("Sending delete request for:" + fileNameD);
  191. JOptionPane.showMessageDialog(null, text);
  192.  
  193. OutputStream os = socket.getOutputStream();
  194. PrintWriter pw = new PrintWriter(os, true);
  195. pw.println("deletefile-" + fileNameD + "-" + sizeS);
  196. pw.flush();
  197. }
  198.  
  199. private static void sendZipFileRequest() throws IOException {
  200. String fileNameD;
  201. // scanner = new Scanner(System.in);
  202. // System.out.println("Please enter file from server to zip
  203. // (e.g.'abc.txt'): ");
  204. // fileNameD = scanner.next();
  205. fileNameD = JOptionPane.showInputDialog("Please enter file to zip (e.g.'abc.txt'): ");
  206. int sizeS = 0;
  207.  
  208. // System.out.println("Sending zip request.");
  209. // System.out.println("name:" + fileNameD);
  210. String text = ("Sending zip request for:" + fileNameD);
  211. JOptionPane.showMessageDialog(null, text);
  212.  
  213. OutputStream os = socket.getOutputStream();
  214. PrintWriter pw = new PrintWriter(os, true);
  215. pw.println("zipfile-" + fileNameD + "-" + sizeS);
  216. pw.flush();
  217. }
  218.  
  219. private static void sendShowArchiveRequest() throws IOException {
  220. String fileNameD = null;
  221. int sizeS = 0;
  222.  
  223. // System.out.println("Sending showarchive request.");
  224. String text = ("Sending showarchive request.");
  225. JOptionPane.showMessageDialog(null, text);
  226.  
  227. OutputStream os = socket.getOutputStream();
  228. PrintWriter pw = new PrintWriter(os, true);
  229. pw.println("showarchive-" + fileNameD + "-" + sizeS);
  230. pw.flush();
  231. }
  232.  
  233. private static void sendReturnFileRequest() throws IOException {
  234. String fileNameD;
  235. // scanner = new Scanner(System.in);
  236. // System.out.println("Please enter file to return (e.g.'abc.txt'): ");
  237. // fileNameD = scanner.next();
  238. fileNameD = JOptionPane.showInputDialog("Please enter file to get from archive (e.g.'abc.txt'): ");
  239. int sizeS = 0;
  240.  
  241. String text = ("Sending return file request for:" + fileNameD);
  242. JOptionPane.showMessageDialog(null, text);
  243. // System.out.println("Sending returnfile request.");
  244. // System.out.println("name:" + fileNameD);
  245.  
  246. OutputStream os = socket.getOutputStream();
  247. PrintWriter pw = new PrintWriter(os, true);
  248. pw.println("returnfile-" + fileNameD + "-" + sizeS);
  249. pw.flush();
  250. }
  251.  
  252. public static void send(String ipAddress, int portNo, String fileLocation) throws IOException {
  253.  
  254. FileInputStream fileInputStream = null;
  255. BufferedInputStream bufferedInputStream = null;
  256.  
  257. // OutputStream outputStream = null;
  258. try {
  259. window.textArea.append("Waiting for receiver...\n");
  260. try {
  261. int partCounter = 1;
  262. int sizeOfFiles = 1024 * 1024;// 1MB
  263. byte[] buffer = new byte[sizeOfFiles];
  264. File file = new File(fileLocation);
  265.  
  266. fileName = file.getName();
  267. int count = 0;
  268.  
  269. try (FileInputStream fis = new FileInputStream(file);
  270. BufferedInputStream bis = new BufferedInputStream(fis)) {
  271. int bytesAmount = 0;
  272. // List<File> listOfFilesD = new ArrayList<File>();
  273.  
  274. while ((bytesAmount = bis.read(buffer)) > 0) {
  275. // write each chunk of data into separate file with
  276. // different number in name
  277. String filePartName = String.format(fileName + "." + partCounter);
  278. File newFile = new File(file.getParent(), filePartName);
  279. FileOutputStream out = new FileOutputStream(newFile);
  280. try {
  281. out.write(buffer, 0, bytesAmount);
  282. count++;
  283. // listOfFilesD.add(newFile);
  284. window.textArea.append("File added" + filePartName + "\n");
  285. list.add("" + filePartName);
  286. out.close();
  287. } catch (Exception e) {
  288. System.out.println(e);
  289. }
  290.  
  291. byte[] byteArray = new byte[(int) newFile.length()];
  292. fileInputStream = new FileInputStream(newFile);
  293. bufferedInputStream = new BufferedInputStream(fileInputStream);
  294. bufferedInputStream.read(byteArray, 0, byteArray.length); // copied
  295. // file
  296. // into
  297. // byteArray
  298.  
  299. // sending file through socket
  300. outputStream = socket.getOutputStream();
  301. window.textArea.append("Sending " + fileLocation + "( size: " + byteArray.length + " bytes)\n");
  302. outputStream.write(byteArray, 0, byteArray.length); // copying
  303. // byteArray
  304. // to
  305. // socket
  306.  
  307. outputStream.flush(); // flushing socket
  308. window.textArea.append("Done.\n"); // file has been sent
  309. partCounter++;
  310.  
  311. }
  312. }
  313.  
  314. } finally {
  315. if (bufferedInputStream != null)
  316. bufferedInputStream.close();
  317. if (outputStream != null)
  318. bufferedInputStream.close();
  319. // if (socket != null)
  320. // socket.close();
  321. }
  322. } catch (IOException e) {
  323. e.printStackTrace();
  324. } finally {
  325. // if (serverSocket != null)
  326. // serverSocket.close();
  327. }
  328.  
  329. }
  330.  
  331. public static void delete() throws IOException {
  332. for (String s : list) {
  333. listString += s + "\t";
  334. }
  335.  
  336. window.textArea.append(listString + "\n");
  337. String fname;
  338. Path p = Paths.get(fileLocation);
  339. Path folder = p.getParent();
  340. // String textPath=(fileLocation+fname);
  341. // Path path = Paths.get(textPath);
  342. for (int i = 0; i < list.size(); i++) {
  343. fname = list.get(i);
  344. window.textArea.append(fname);
  345. // String[] parts = fname.split(".");
  346. // String nu = parts[2];
  347. String textPath = (folder + "\\" + fname);
  348. Path path = Paths.get(textPath);
  349. try {
  350. Files.delete(path);
  351. } catch (NoSuchFileException x) {
  352. System.err.format("%s: no such" + " file or directory%n", path);
  353. } catch (DirectoryNotEmptyException x) {
  354. System.err.format("%s not empty%n", path);
  355. } catch (IOException x) {
  356. // File permission problems are caught here.
  357. System.err.println(x);
  358. }
  359. }
  360. }
  361.  
  362. public static void receiveReturnedFile(String fileLocation, int sizeR) throws IOException {
  363. window.textArea.append("Saving file...");
  364. fileLocation = ("C:\\Users\\Dominika\\Desktop\\JAVAR\\" + fileName);
  365. DataInputStream dis = new DataInputStream(socket.getInputStream());
  366. FileOutputStream fos = new FileOutputStream(fileLocation);
  367. byte[] buffer = new byte[sizeR];
  368.  
  369. window.textArea.append("rozmiar" + sizeR);
  370. window.textArea.append("Nazwa" + fileLocation);
  371. int read = 0;
  372. int totalRead = 0;
  373. int remaining = sizeR;
  374. try {
  375. while ((read = dis.read(buffer, 0, Math.min(buffer.length, remaining))) > 0) {
  376. totalRead += read;
  377. remaining -= read;
  378. fos.write(buffer, 0, read);
  379. }
  380. } catch (IOException e) {
  381. // TODO Auto-generated catch block
  382. e.printStackTrace();
  383. }
  384.  
  385. fos.close();
  386. // dis.close();
  387. }
  388.  
  389. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement