S
S
SuperDuperPuper2021-10-06 11:49:42
Java
SuperDuperPuper, 2021-10-06 11:49:42

A broken picture comes from the server. How to fix it?

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
import java.util.List;

public class Server extends  Thread{
    Socket socket;
    public static void main (String [] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(8000);
            while (true) {
                new Server(serverSocket.accept());
            }
        }
        catch (IOException ex) {System.out.println("IO error!");}
    }
    Server (Socket socket){
        this.socket = socket;
        setDaemon(true);
        start();
    }
    String getPath(String request) {
        String[] arr = request.split(" ");
        //"/home/nikita/IdeaProjects/Web_Platform/src
        return "./src/web" + arr[1];
    }
    public void run (){
        String response = "";
        try{
            InputStream inputStream = socket.getInputStream();
            OutputStream outputStream = socket.getOutputStream();

            byte [] buf = new byte[1024*1024*5];
            int bytes = inputStream.read(buf); // читает байты и передает в buf байты ,а потом возвращает кол-во байтов
            String request = new String(buf,0,buf.length);
            String path = getPath(request);
            File file = new File (path);

            System.out.println(request + '\n');
            boolean exists = file.exists();
            if (exists) {
                byte[] data ={};
                String content_type = "text/html";
                if (path.contains("png")) {
                    content_type = "image/png";
                    BufferedImage image = ImageIO.read(file);
                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    ImageIO.write(image, "png", byteArrayOutputStream);
                    data = byteArrayOutputStream.toByteArray();
                }
                else {
                    FileInputStream input = new FileInputStream(file);
                    data = input.readAllBytes();
                }



                response += "HTTP/1.1 200 OK\r\n";
                response+= "Date: "  + new Date() + "\r\n";
                response +="Content-Type: " + content_type + "\r\n";

                response +="Connection: close\r\n";

                response +="Server: Server\r\n";
                response +="Pragma: no-cache\r\n\r\n";
                response+= new String(data);

                //System.out.println(response);
                //outputStream.write(response.getBytes());

            }
            else {
                response += "HTTP/1.1 404 Not Found\n";
                response+= "Date: "  + new Date() + "\n";
                response +="Content-Type: text/plain\n";
                response +="Connection: close\n";

                response +="Server: Server\n";
                response +="Pragma: no-cache\n\n";
                response += "File " + path + " Not Found!\n\nIts not  normal\nS";
                response+="end";

            }

            outputStream.write(response.getBytes());
            outputStream.flush();
            outputStream.close();
            inputStream.close();
            socket.close();
            return;


            }
        catch (IOException ex){
            System.out.println("Error: " + ex.toString());
        }
    }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SuperDuperPuper, 2021-10-08
@SuperDuperPuper

I understood what the problem is. It consists in this line: response+= new String(data);
new String() - mangled bytes. Therefore, I created a byte array, where I copied the byte array with the answer and the byte array with the picture.

byte[] responseBytes  = response.getBytes();
 byte[] finalResponse = new byte[responseBytes.length + data.length];
 System.arraycopy(responseBytes,0,finalResponse, 0,responseBytes.length);
System.arraycopy(data,0,finalResponse, responseBytes.length, data.length);
outputStream.write(finalResponse);

Now everything is working

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question