Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package mailpractice;
- import java.io.IOException;
- import java.net.ServerSocket;
- import java.net.Socket;
- /**
- *
- * @author Heisenberg
- */
- public class finalServer {
- private static int workerThreadNo, id;
- public static void main(String[] args) throws IOException {
- workerThreadNo = 0;
- id = 0 ;
- int port = 6789;
- ServerSocket ss = new ServerSocket(port);
- System.out.println("Server Socket at port " + port + " is opened successfully.");
- while(true)
- {
- Socket s = ss.accept();
- finalHTTPWorker worker = new finalHTTPWorker(s, id);
- Thread t = new Thread(worker);
- t.start();
- workerThreadNo++;
- id++;
- }
- }
- }
- public class finalHTTPWorker implements Runnable {
- private Socket socket;
- InputStream is = null;
- OutputStream os = null;
- int id;
- public finalHTTPWorker(Socket s, int id) throws IOException {
- this.socket = s;
- this.id = id;
- is = s.getInputStream();
- os = s.getOutputStream();
- }
- @Override
- public void run() {
- BufferedReader br = new BufferedReader(new InputStreamReader(is));
- PrintWriter pw = new PrintWriter(os);
- // while (true) {
- try {
- String str = br.readLine(), fileName;
- /*GET /ihi.jpg HTTP/1.1*/
- // StringTokenizer tokenizer = new StringTokenizer(str, "/");
- // fileName = tokenizer.nextToken();
- // if (tokenizer.hasMoreTokens()) {
- //
- // fileName = tokenizer.nextToken();
- // }
- //
- // fileName = fileName.replace(" HTTP", "");
- //
- // if (fileName.equals("")) {
- // fileName = "index.html";
- // }
- pw.println("HTTP/1.0 200 OK");
- pw.flush();
- System.out.println(br.readLine());
- pw.println("MIME-Version: 1.0");
- pw.flush();
- pw.println("Content-Type: image/png");
- pw.flush();
- System.out.println(br.readLine());
- fileName = "riko.png";
- System.out.println(fileName);
- File file = new File(fileName);
- byte[] content;
- FileInputStream fis = new FileInputStream(file);
- BufferedInputStream bis = new BufferedInputStream(fis);
- OutputStream sendFileStream = os;
- int fileLength = (int) file.length();
- pw.println("Content-Length: " + fileLength);
- pw.flush();
- pw.println("\r\n");
- pw.flush();
- System.out.println(br.readLine());
- long current = 0;
- while (current != fileLength) {
- int size = 10000;
- if (fileLength - current >= size) {
- current += size;
- } else {
- size = (int) (fileLength - current);
- current = fileLength;
- }
- content = new byte[size];
- bis.read(content, 0, size);
- sendFileStream.write(content);
- //System.out.println("Hiiii");
- }
- sendFileStream.flush();
- System.out.println("File sent successfully!");
- is.close();
- os.close();
- socket.close();
- } catch (IOException ex) {
- Logger.getLogger(finalHTTPWorker.class.getName()).log(Level.SEVERE, null, ex);
- }
- // }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement