在java http中获取图像的二进制文件

时间:2014-02-02 20:14:25

标签: java http

我需要读取一个图像文件并使用HTTP协议将其发送到Web浏览器,我无法弄清楚如何发送图像的字节。我不能为我的生活弄清楚。提前致谢。这是我的代码:

import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

import javax.imageio.ImageIO;


public class Main {


    byte[] byt=null;

    public String read(String message){
        String httpHeader="";
        String toReturn="";
        try{
            if(message!=null){
                String[] parts=message.split("\n");
                String[] RequstParts=parts[0].split(" ");
                System.out.println("This is a "+RequstParts[0]+" request for "+RequstParts[1]);
                if(RequstParts[1].equals("/"))RequstParts[1]="index.html";
                if(RequstParts[0].equals("GET")){
                    if(RequstParts[1].endsWith(".html")){
                        httpHeader="HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
                        BufferedReader in = new BufferedReader(new FileReader(new File("WebContent/"+RequstParts[1])));
                        String line="";
                        while(null!=(line=in.readLine())){
                            toReturn+=line;
                        }
                    }else if(RequstParts[1].endsWith(".jpg")){

                        httpHeader="HTTP/1.1 200 OK\r\nContent-Type: image/jpg\r\nContent-Length: 13312\r\n\r\n";

                    }
                }
            }
        }catch(Exception e){
            toReturn+="<br>ERROR: "+e.toString();
            return httpHeader+toReturn;
        }
        return httpHeader+toReturn;
    }

    public Main(){
        try{
            ServerSocket listener = new ServerSocket(9090);
            System.out.println("HTTP server started!");
            while(true){
                 Socket socket = listener.accept();
                 System.out.println("\nRecieved Data!");
                 BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                 String message=read(in.readLine());
                 PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
                 System.out.println("\nSENDING "+message);
                 out.println(message);
                 out.println(byt);
                 out.close();
            }
        }catch(Exception e){
            System.out.println("ERROR "+e.toString());
            e.printStackTrace();
        }
    }

    public static void main(String args[]){
        new Main();

    }
}

1 个答案:

答案 0 :(得分:0)

这应该有效:

public byte[] extractBytes (String ImageName) throws IOException {
 // open image
 File imgPath = new File(ImageName);
 BufferedImage bufferedImage = ImageIO.read(imgPath);

 // get DataBufferBytes from Raster
 WritableRaster raster = bufferedImage .getRaster();
 DataBufferByte data   = (DataBufferByte) raster.getDataBuffer();

 return ( data.getData() );
}

所以在图像上调用此extractBytes(在此处指定方法的文件名),并使用以下命令发送返回的byte[]

OutputStream socketOutputStream = socket.getOutputStream();
socketOutputStream.write(byte[]);

取自here的方法代码。