数日前、HTTP 経由で画像を直接転送するように独自の画像クライアントをプログラミングすることを考えました。私はかなりの時間をグーグルで調べて調査し、サーバーを書きました:
public class SConnection extends Thread {
Socket client;
/* ... */
@Override
public void run() {
while(true) {
try {
//Get some image paths
File folder = new File(new java.net.URI("file:///C:/images/"));
File[] images = folder.listFiles();
//Load the image
BufferedImage bi = ImageIO.read(images[0]);
//Write the image
ImageIO.write(bi, "JPEG", client.getOutputStream());
} catch (Exception ex) {
ex.printStackTrace();
return;
}
}
}
メインクラスは、多くの接続を受け入れるのを待っている Thread であり、それらを ArrayList に格納し、SConnection のインスタンスを作成して開始します。
クライアントは次のようになります。
URL target = new URL("http://127.0.0.1:82"); //The server - so far, so good
URLConnection conn = target.openConnection();
BufferedImage in = ImageIO.read(conn.getInputStream()); //And as I try to receive the image: boom, exception
File save = new File(new java.net.URI("file:///C:/images/result.jpeg"));
ImageIO.write(in, "JPEG", save);
サーバーとクライアントの両方が、ImageIO.write / ImageIO.read - 行にある例外を送信します。
サーバーは次のように述べています。
java.net.SocketException: Connection reset by peer: socket write error
クライアントは次のように述べています。
java.io.IOException: Invalid Http response
画像が正しく転送されていないことがわかりましたが、何を変更すればよいですか? 手がかりはありますか?
よろしくお願いします。