Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.25 KB | None | 0 0
  1. import java.io.BufferedInputStream;
  2. import java.io.BufferedOutputStream;
  3. import java.io.BufferedReader;
  4. import java.io.DataInputStream;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.io.InputStreamReader;
  11. import java.io.OutputStream;
  12. import java.io.PrintWriter;
  13. import java.net.Socket;
  14. import java.nio.file.Path;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. import java.util.Scanner;
  18. import java.util.zip.ZipEntry;
  19. import java.util.zip.ZipOutputStream;
  20.  
  21. public class ServerThread extends Thread {
  22. public static Socket socket;
  23. public static Scanner scanner;
  24. public static int size;
  25. public static long sizeR;
  26. public static String request;
  27. public static String requestM;
  28. public static String fileName;
  29. public static String fileLocation;
  30. public static Path path = null;
  31. public static String listString = "";
  32.  
  33. ServerThread(Socket socket) {
  34. this.socket = socket;
  35. }
  36.  
  37. public void run() {
  38. try {
  39. while (true) {
  40. InputStream is = socket.getInputStream();
  41. OutputStream os = socket.getOutputStream();
  42. PrintWriter pw = new PrintWriter(os, true);
  43. BufferedReader br = new BufferedReader(new InputStreamReader(is));
  44. String request = br.readLine();
  45. if (request != null) {
  46. System.out.println("Client request: " + request);
  47. processRequest(request, pw, is);
  48. }
  49. }
  50. } catch (IOException e) {
  51. e.printStackTrace();
  52. }
  53. }
  54.  
  55. public void processRequest(String request, PrintWriter pw, InputStream is) throws IOException {
  56. String[] parts = request.split("-");
  57. requestM = parts[0];
  58. fileName = parts[1];
  59. String sizeS = parts[2];
  60. size = Integer.valueOf(sizeS);
  61. System.out.println("File" + fileName);
  62.  
  63. if (requestM.equals("savefile")) {
  64. File f = new File("C:\\Users\\Pacia\\Desktop\\JAVA\\" + fileName);
  65. // File f = new File("C:\\Users\\Dominika\\Desktop\\JAVA2\\" + fileName);
  66. if(f.exists()){
  67. System.out.println("file exists");
  68. pw.println("File exists.");
  69. pw.flush();
  70. }
  71. else{
  72. pw.println("notexist");
  73. pw.flush();
  74. receiveFile(fileLocation, size);
  75. System.out.println("File saved.");
  76. pw.println("File saved.");
  77. pw.flush();
  78. }
  79.  
  80. } else if (requestM.equals("deletefile")) {
  81. deleteFiles(fileLocation);
  82. System.out.println("File deleted.");
  83. pw.println("File deleted.");
  84. pw.flush();
  85. }else if (requestM.equals("zipfile")) {
  86. zipFile(fileLocation);
  87. System.out.println("File ziped.");
  88. pw.println("File ziped.");
  89. pw.flush();
  90. } else if (requestM.equals("showarchive")) {
  91. listOfArchive(new File("C:\\Users\\Pacia\\Desktop\\JAVA"));
  92. // listOfArchive(new File("C:\\Users\\Dominika\\Desktop\\JAVA2"));
  93. System.out.println("List shown");
  94. pw.println("" + listString);
  95. pw.flush();
  96. } else if (requestM.equals("returnfile")) {
  97. sendSaveRequest(fileName);
  98. returnFile(fileLocation);
  99. System.out.println("File returned");
  100. pw.println("File returned.");
  101. pw.flush();
  102. } else {
  103. System.out.println("Option doesn't exist");
  104. }
  105. }
  106.  
  107. private void sendSaveRequest(String fileName) throws IOException {
  108. fileLocation = ("C:\\Users\\Pacia\\Desktop\\JAVA\\" + fileName);
  109. File file = new File(fileLocation);
  110. sizeR = file.length();
  111. String sizeS = String.valueOf(sizeR);
  112.  
  113. System.out.println("name:" + fileName);
  114. System.out.println("size:" + sizeS);
  115.  
  116. OutputStream os = socket.getOutputStream();
  117. PrintWriter pw = new PrintWriter(os, true);
  118. pw.println(fileName + "-" + sizeS);
  119. pw.flush();
  120. }
  121.  
  122. public void receiveFile(String fileLocation, int size) throws IOException {
  123. System.out.println("Saving file...");
  124. fileLocation = ("C:\\Users\\Pacia\\Desktop\\JAVA\\" + fileName);
  125. //fileLocation = ("C:\\Users\\Dominika\\Desktop\\JAVA2\\" + fileName);
  126. DataInputStream dis = new DataInputStream(socket.getInputStream());
  127. FileOutputStream fos = new FileOutputStream(fileLocation);
  128. byte[] buffer = new byte[size];
  129.  
  130. System.out.println("rozmiar" + size);
  131. System.out.println("Nazwa" + fileLocation);
  132. int read = 0;
  133. int totalRead = 0;
  134. int remaining = size;
  135. try {
  136. while ((read = dis.read(buffer, 0, Math.min(buffer.length, remaining))) > 0) {
  137. totalRead += read;
  138. remaining -= read;
  139. fos.write(buffer, 0, read);
  140. }
  141. } catch (IOException e) {
  142. // TODO Auto-generated catch block
  143. e.printStackTrace();
  144. }
  145.  
  146. fos.close();
  147. // dis.close();
  148. }
  149.  
  150. public static boolean deleteFiles(String fileLocation) {
  151. System.out.println("Called deleteFiles");
  152. // fileLocation = ("C:\\Users\\Pacia\\Desktop\\JAVA\\" + fileName);
  153. fileLocation = ("C:\\Users\\Dominika\\Desktop\\JAVA2\\" + fileName);
  154. File file = new File(fileLocation);
  155.  
  156. if (file.delete()) {
  157. System.out.println("File deleted successfully");
  158. } else {
  159. System.out.println("Failed to delete the file");
  160. }
  161. return true;
  162. }
  163.  
  164. public static List<File> listOfArchive(File catalog) {
  165. System.out.println(catalog.getAbsoluteFile());
  166. List<File> listOfFiles = new ArrayList<File>();
  167. ArrayList<String> list = new ArrayList<String>();
  168. System.out.println("List of files in archive");
  169.  
  170. if (catalog.isDirectory()) {
  171. String[] subNote = catalog.list();
  172. for (String filename : subNote) {
  173. listOfFiles.add(new File(catalog.getAbsoluteFile() + "//" + filename));
  174. System.out.println("" + filename);
  175. list.add("" + filename);
  176. }
  177. }
  178. for (String s : list) {
  179. listString += s + "\t";
  180. }
  181. return listOfFiles;
  182. }
  183.  
  184. public static void returnFile(String fileLocation) throws IOException {
  185.  
  186. FileInputStream fileInputStream = null;
  187. BufferedInputStream bufferedInputStream = null;
  188. OutputStream outputStream = null;
  189.  
  190. try {
  191. System.out.println("Waiting for client to receiver...");
  192. try {
  193. int partCounter = 1;
  194. int sizeOfFiles = 1024 * 1024;// 1MB
  195. byte[] buffer = new byte[sizeOfFiles];
  196. // fileLocation = ("C:\\Users\\Pacia\\Desktop\\JAVA\\" + fileName);
  197. fileLocation = ("C:\\Users\\Dominika\\Desktop\\JAVA2\\" + fileName);
  198. File file = new File(fileLocation);
  199. // fileName = file.getName();
  200. int count = 0;
  201.  
  202. try (FileInputStream fis = new FileInputStream(file);
  203. BufferedInputStream bis = new BufferedInputStream(fis)) {
  204. int bytesAmount = 0;
  205.  
  206. while ((bytesAmount = bis.read(buffer)) > 0) {
  207. String filePartName = String.format(fileName + "." + partCounter);
  208. File newFile = new File(file.getParent(), filePartName);
  209. try (FileOutputStream out = new FileOutputStream(newFile)) {
  210. out.write(buffer, 0, bytesAmount);
  211. count++;
  212. }
  213. byte[] byteArray = new byte[(int) newFile.length()];
  214. fileInputStream = new FileInputStream(newFile);
  215. bufferedInputStream = new BufferedInputStream(fileInputStream);
  216. bufferedInputStream.read(byteArray, 0, byteArray.length); // copied file into byteArray
  217.  
  218. // sending file through socket
  219. outputStream = socket.getOutputStream();
  220. System.out.println("Sending " + fileLocation + "( size: " + byteArray.length + " bytes)");
  221. outputStream.write(byteArray, 0, byteArray.length);
  222.  
  223. outputStream.flush();
  224. System.out.println("Done.");
  225. partCounter++;
  226. }
  227.  
  228. }
  229.  
  230. } finally {
  231. if (bufferedInputStream != null)
  232. bufferedInputStream.close();
  233. if (outputStream != null)
  234. bufferedInputStream.close();
  235. }
  236. } catch (IOException e) {
  237. e.printStackTrace();
  238. }
  239. }
  240.  
  241. public boolean zipFile(String fileLocation) throws IOException {
  242.  
  243. // fileLocation = ("C:\\Users\\Dominika\\Desktop\\JAVA2\\" + fileName);
  244. fileLocation = ("C:\\Users\\Pacia\\Desktop\\JAVA\\" + fileName);
  245. File file = new File(fileLocation);
  246. FileInputStream in = new FileInputStream(file);
  247.  
  248. // out put file
  249. String fileNameZip = "C:\\Users\\Dominika\\Desktop\\JAVA2\\" + file.getName() + ".zip";
  250. System.out.println("Plik zip: " + fileNameZip);
  251. ZipOutputStream out = new ZipOutputStream(new FileOutputStream(fileNameZip));
  252.  
  253. // name the file inside the zip file
  254. out.putNextEntry(new ZipEntry(file.getName()));
  255.  
  256. byte[] b = new byte[1024];
  257. int count;
  258.  
  259. while ((count = in.read(b)) > 0) {
  260. out.write(b, 0, count);
  261. }
  262. out.close();
  263. in.close();
  264. return true;
  265. }
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement