Advertisement
Guest User

Untitled

a guest
Mar 24th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. /////////////////MAIN///////////////////////////
  2.  
  3.  
  4.  
  5. package Practica1;
  6.  
  7.  
  8. import java.io.BufferedInputStream;
  9.  
  10. import java.io.BufferedOutputStream;
  11. import java.io.BufferedReader;
  12. import java.io.File;
  13. import java.io.FileInputStream;
  14. import java.io.FileOutputStream;
  15. import java.io.FilterInputStream;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.io.InputStreamReader;
  19. import java.io.OutputStream;
  20. import java.net.ServerSocket;
  21. import java.net.Socket;
  22. import java.util.concurrent.TimeUnit;
  23.  
  24.  
  25.  
  26. public class WServer extends Thread{
  27.  
  28. private static final int PORT = 9008;
  29.  
  30. public static void main(String[] args) throws Exception{
  31.  
  32.  
  33. new Thread() {
  34. @Override
  35. public void run() {
  36.  
  37. try{
  38. Socket con;
  39. System.out.println("Conectat");
  40. BufferedInputStream bis;
  41. BufferedOutputStream bos;
  42. BufferedReader bfr;
  43. InputStream is;
  44. OutputStream os;
  45. HttpRequest hr;
  46. GzipZip gz;
  47.  
  48. try {
  49.  
  50. ServerSocket server = new ServerSocket(PORT);
  51.  
  52. while (true) {
  53. con = server.accept();
  54. is = con.getInputStream();
  55. os = con.getOutputStream();
  56. hr = new HttpRequest();
  57. bfr = new BufferedReader(new InputStreamReader(is));
  58. System.out.println("Esperant");
  59.  
  60.  
  61. String filex;
  62. filex = bfr.readLine();
  63. String str = hr.getPath(filex);
  64. InputStream fis = new FileInputStream(str);
  65. File convf = new File(str);
  66.  
  67.  
  68. if (hr.asc == true) {
  69. gz = new GzipZip(fis);
  70. // gz.AsciiInputStream(in);
  71. } else if (hr.gzip == true) {
  72. System.out.println("Compress Gzip");
  73. gz = new GzipZip(fis);
  74. gz.compressGZIP(convf, str);
  75. }else if(hr.zip == true){
  76. System.out.println("Compress Zip");
  77. gz = new GzipZip(fis);
  78. gz.compressZip(str, "CompressZ.zip");
  79. }
  80.  
  81. String httpResponse = "HTTP/1.1 200 OK\r\n\r\n";
  82. os.write(httpResponse.getBytes("UTF-8"));
  83.  
  84. System.out.println("Surt While");
  85.  
  86.  
  87. int x;
  88. while ((x = fis.read()) != -1) {
  89. os.write(x);
  90. //System.out.println(os);
  91.  
  92. }
  93.  
  94. fis.close();
  95. is.close();
  96. os.close();
  97. con.close();
  98. System.out.println("Tanca");
  99.  
  100. }
  101.  
  102. } catch (Exception e) {
  103.  
  104. e.printStackTrace();
  105. }
  106.  
  107. } catch (Exception e) {
  108. e.printStackTrace();
  109. }
  110. }
  111. }.start();
  112.  
  113. }
  114. }
  115.  
  116.  
  117. /////////////////////////gzipZip//////////////////////
  118.  
  119. package Practica1;
  120.  
  121.  
  122. import java.io.BufferedReader;
  123. import java.io.ByteArrayInputStream;
  124. import java.io.ByteArrayOutputStream;
  125. import java.io.File;
  126. import java.io.FileInputStream;
  127. import java.io.FileOutputStream;
  128. import java.io.FilterInputStream;
  129. import java.io.IOException;
  130. import java.io.InputStream;
  131. import java.io.InputStreamReader;
  132. import java.util.zip.GZIPInputStream;
  133. import java.util.zip.GZIPOutputStream;
  134. import java.util.zip.ZipEntry;
  135. import java.util.zip.ZipInputStream;
  136. import java.util.zip.ZipOutputStream;
  137.  
  138. import javax.xml.crypto.dsig.spec.XPathType.Filter;
  139.  
  140.  
  141. public class GzipZip extends FilterInputStream{
  142.  
  143.  
  144. public static void compressGZIP(File input, String output) throws IOException {
  145. try (GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(output))) {
  146. try (FileInputStream in = new FileInputStream(input)) {
  147. byte[] buffer = new byte[1024];
  148. int len;
  149. while ((len = in.read(buffer)) != -1) {
  150. out.write(buffer, 0, len);
  151. }
  152. }
  153. }
  154. }
  155.  
  156.  
  157. public static void compressZip(String input, String output) throws IOException {
  158. FileInputStream in = new FileInputStream(input);
  159. FileOutputStream out = new FileOutputStream(output);
  160. byte[] buffer = new byte [1024];
  161. ZipOutputStream zout = new ZipOutputStream(out);
  162. ZipEntry entry = new ZipEntry(input);
  163. int len;
  164. while ((len = in.read(buffer)) != -1) {
  165. zout.write(buffer, 0, len);
  166. }
  167. zout.closeEntry();
  168. zout.close();
  169. }
  170.  
  171. /*public int AsciiInputStream(InputStream fis) throws IOException{
  172. char aux;
  173. int n = 0;
  174. int x = -2;
  175.  
  176.  
  177. do {
  178. n = fis.read();
  179.  
  180. aux = (char) n;
  181.  
  182. if (st == false && aux== '<') {
  183. st = true;
  184. return x;
  185. }else if (st == false) {
  186. return x;
  187. }else if (aux == '>') {
  188. st = false;
  189.  
  190. }
  191. }while(n != -1);
  192. return x;
  193. }*/
  194.  
  195.  
  196. protected GzipZip(InputStream arg0) {
  197. super(arg0);
  198. }
  199.  
  200. @Override
  201. public int read() throws IOException {
  202. int readchr = super.read();
  203. while(readchr == '<') {
  204. readchr = AsciiInputStream(readchr);
  205. }
  206. return readchr;
  207. }
  208.  
  209. private boolean inTag = false;
  210.  
  211. public int AsciiInputStream(int readchr) throws IOException {
  212. int chr = super.read();
  213.  
  214. if(chr == '<'){
  215. inTag = true;
  216. }else if(chr == '>'){
  217. inTag = false;
  218. }else if(!inTag){
  219. return chr;
  220. }
  221.  
  222. return readchr;
  223. }
  224. }
  225.  
  226.  
  227. ////////////////////////////request/////////////////////////////
  228.  
  229. package Practica1;
  230.  
  231.  
  232. public class HttpRequest{
  233.  
  234.  
  235.  
  236. boolean asc= false;
  237. boolean gzip = false;
  238. boolean zip = false;
  239.  
  240. String getPath (String httpRequest){
  241.  
  242.  
  243. if(httpRequest.contains("asc=true")){
  244. asc = true;
  245. }else if(httpRequest.contains("gzip=true")){
  246. gzip=true;
  247. }else if(httpRequest.contains("zip=true")){
  248. zip=true;
  249. }
  250.  
  251. int endindex = httpRequest.indexOf("?");
  252. int beginIndex = httpRequest.indexOf("/")+1;
  253.  
  254.  
  255. System.out.println(httpRequest.substring(beginIndex,endindex));
  256. return httpRequest.substring(beginIndex,endindex);
  257.  
  258. }
  259.  
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement