问题描述:

I have a server that displays what the user asks for from the browser, I am using linux and when i run the server and ask for a file like Image.png using this link localhost:9999/Image.png on FireFox i get this message:

The image "localhost:9999/Image.png" cannot be displayed because it

contains errors.

But when i change the variable fileName to an HTML file it works perfectly and i can visualize the html page.

What am I doing wrong??

This is my server:

import java.io.*;

import java.net.*;

import java.util.Scanner;

public class Server {

public static void main(String args[]) throws IOException {

// Declarem les variables a utilitzar

ServerSocket serverSocket = null;

Socket socket = null;

InputStream inS = null;

OutputStream outS = null;

try

{

serverSocket = new ServerSocket(9999);

while(true)

{

socket= serverSocket.accept();

inS = socket.getInputStream();

outS = socket.getOutputStream();

try{

BufferedReader br = new BufferedReader(new InputStreamReader(inS));

BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(outS));

System.out.println("THis is what the user wants = " + br.readLine());

String fileName = "Image.png";

String extension= "";

int i = fileName.lastIndexOf('.');

if (i > 0) {

extension = fileName.substring(i+1);

}

String dataReturn = "";

if(extension.equals("png"))

{

bw.write("HTTP/1.0 200 OK\r\n");

bw.write("Content-Type: image/png\r\n");

bw.write("\r\n");

FileReader myFilepng = new FileReader(fileName);

Scanner scanner1 = new Scanner(myFilepng);

dataReturn = "";

while(scanner1.hasNextLine()) {

dataReturn = scanner1.nextLine();

System.out.println(dataReturn);

bw.write(dataReturn);

}

scanner1.close();

}else{

if(extension.equals("html"))

{

bw.write("HTTP/1.0 200 OK\r\n");

bw.write("Content-Type: text/html\r\n");

bw.write("\r\n");

bw.write("<TITLE>"+fileName+"/TITLE>");

FileReader myFile = new FileReader(fileName);

Scanner scanner = new Scanner(myFile);

dataReturn = "";

while(scanner.hasNextLine()) {

dataReturn = scanner.nextLine();

System.out.println(dataReturn);

bw.write(dataReturn);

}

scanner.close();

}

}

bw.close();

}catch(Exception e)

{

}

}

}catch (IOException e) {

System.out.println(e);

}

inS.close();

outS.close();

socket.close();

}

}

网友答案:

You are not writing the contents of your png file to your bw BufferedWriter. Instead you are only sending the header of the response to the client. As you are indicating your response is a png image and there is no data, your browser is telling you the image contains errors (in fact, it does not contains nothing at all).

Open the png filename, write the data to your "bw" buffer to send it to the client. That should be enough.

Edit:

To to that, try the following code for your "if" is image:

if(extension.equals("png"))
{
    File file = new File(fileName);
    FileInputStream fis = new FileInputStream(file);
    byte[] data = new byte[(int) file.length()];
    fis.read(data);
    fis.close();

    DataOutputStream binaryOut = new DataOutputStream(outS);
    binaryOut.writeBytes("HTTP/1.0 200 OK\r\n");
    binaryOut.writeBytes("Content-Type: image/png\r\n");
    binaryOut.writeBytes("Content-Length: " + data.length);
    binaryOut.writeBytes("\r\n\r\n");
    binaryOut.write(data);

    binaryOut.close();
}

Note the use of a binary stream in comparison to the text stream you use in case of html.

相关阅读:
Top