Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /////////////////MAIN///////////////////////////
- package Practica1;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.FilterInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.concurrent.TimeUnit;
- public class WServer extends Thread{
- private static final int PORT = 9008;
- public static void main(String[] args) throws Exception{
- new Thread() {
- @Override
- public void run() {
- try{
- Socket con;
- System.out.println("Conectat");
- BufferedInputStream bis;
- BufferedOutputStream bos;
- BufferedReader bfr;
- InputStream is;
- OutputStream os;
- HttpRequest hr;
- GzipZip gz;
- try {
- ServerSocket server = new ServerSocket(PORT);
- while (true) {
- con = server.accept();
- is = con.getInputStream();
- os = con.getOutputStream();
- hr = new HttpRequest();
- bfr = new BufferedReader(new InputStreamReader(is));
- System.out.println("Esperant");
- String filex;
- filex = bfr.readLine();
- String str = hr.getPath(filex);
- InputStream fis = new FileInputStream(str);
- File convf = new File(str);
- if (hr.asc == true) {
- gz = new GzipZip(fis);
- // gz.AsciiInputStream(in);
- } else if (hr.gzip == true) {
- System.out.println("Compress Gzip");
- gz = new GzipZip(fis);
- gz.compressGZIP(convf, str);
- }else if(hr.zip == true){
- System.out.println("Compress Zip");
- gz = new GzipZip(fis);
- gz.compressZip(str, "CompressZ.zip");
- }
- String httpResponse = "HTTP/1.1 200 OK\r\n\r\n";
- os.write(httpResponse.getBytes("UTF-8"));
- System.out.println("Surt While");
- int x;
- while ((x = fis.read()) != -1) {
- os.write(x);
- //System.out.println(os);
- }
- fis.close();
- is.close();
- os.close();
- con.close();
- System.out.println("Tanca");
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }.start();
- }
- }
- /////////////////////////gzipZip//////////////////////
- package Practica1;
- import java.io.BufferedReader;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.FilterInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.util.zip.GZIPInputStream;
- import java.util.zip.GZIPOutputStream;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipInputStream;
- import java.util.zip.ZipOutputStream;
- import javax.xml.crypto.dsig.spec.XPathType.Filter;
- public class GzipZip extends FilterInputStream{
- public static void compressGZIP(File input, String output) throws IOException {
- try (GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(output))) {
- try (FileInputStream in = new FileInputStream(input)) {
- byte[] buffer = new byte[1024];
- int len;
- while ((len = in.read(buffer)) != -1) {
- out.write(buffer, 0, len);
- }
- }
- }
- }
- public static void compressZip(String input, String output) throws IOException {
- FileInputStream in = new FileInputStream(input);
- FileOutputStream out = new FileOutputStream(output);
- byte[] buffer = new byte [1024];
- ZipOutputStream zout = new ZipOutputStream(out);
- ZipEntry entry = new ZipEntry(input);
- int len;
- while ((len = in.read(buffer)) != -1) {
- zout.write(buffer, 0, len);
- }
- zout.closeEntry();
- zout.close();
- }
- /*public int AsciiInputStream(InputStream fis) throws IOException{
- char aux;
- int n = 0;
- int x = -2;
- do {
- n = fis.read();
- aux = (char) n;
- if (st == false && aux== '<') {
- st = true;
- return x;
- }else if (st == false) {
- return x;
- }else if (aux == '>') {
- st = false;
- }
- }while(n != -1);
- return x;
- }*/
- protected GzipZip(InputStream arg0) {
- super(arg0);
- }
- @Override
- public int read() throws IOException {
- int readchr = super.read();
- while(readchr == '<') {
- readchr = AsciiInputStream(readchr);
- }
- return readchr;
- }
- private boolean inTag = false;
- public int AsciiInputStream(int readchr) throws IOException {
- int chr = super.read();
- if(chr == '<'){
- inTag = true;
- }else if(chr == '>'){
- inTag = false;
- }else if(!inTag){
- return chr;
- }
- return readchr;
- }
- }
- ////////////////////////////request/////////////////////////////
- package Practica1;
- public class HttpRequest{
- boolean asc= false;
- boolean gzip = false;
- boolean zip = false;
- String getPath (String httpRequest){
- if(httpRequest.contains("asc=true")){
- asc = true;
- }else if(httpRequest.contains("gzip=true")){
- gzip=true;
- }else if(httpRequest.contains("zip=true")){
- zip=true;
- }
- int endindex = httpRequest.indexOf("?");
- int beginIndex = httpRequest.indexOf("/")+1;
- System.out.println(httpRequest.substring(beginIndex,endindex));
- return httpRequest.substring(beginIndex,endindex);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement