使用ObjectOutputStream通过Socket发送多个文件

时间:2015-08-23 15:29:55

标签: java android sockets objectinputstream objectoutputstream

我创建了一个应用程序,我必须一个接一个地发送多个图像,然后在while循环中使用Object(输出/输入)Stream。但它只发送一个文件,我猜它是第一个图像。我可以使用对象(输出/输入)流发送多个文件吗?如果是这样,我的代码有什么问题?

服务器:(从PC发送屏幕截图)

package application;

import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;

import javax.imageio.ImageIO;

public class ScreenCapture {

    Socket socket;
    ObjectOutputStream os;
    Robot robot;
    PrintStream ps;

    public ScreenCapture() throws IOException, AWTException {
        // TODO Auto-generated constructor stub
        socket = SocketWrapper.getSocket();
        os = new ObjectOutputStream(socket.getOutputStream());
        robot = new Robot();
        ps = new PrintStream(socket.getOutputStream());
        new Record().start();

    }

    private class Record extends Thread{
        @Override
        public void run() {
            while(true){
                getScreenShot();
                FileInputStream fis;
                try {
                    File f = new File("/Users/Tomahawk/Documents/send.jpg");
                    fis = new FileInputStream(f);
                    byte[] byteArray = new byte[fis.available()];

                    fis.read(byteArray);

                    os.writeObject(byteArray);
                    os.flush();

                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                System.out.println("Sent File");
            }
        }
    }

    public void getScreenShot(){
        Rectangle rect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
        BufferedImage img = robot.createScreenCapture(rect);

        try {
            ImageIO.write(img, "jpg", new File("/Users/Tomahawk/Documents/send.jpg"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

客户:(接收屏幕截图并将其保存在手机上)

package com.pcontrol.tomahawk.pcontrol;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class ScreenCapture extends Activity {

    Socket socket;
    ObjectInputStream is;
    OutputStream os;
    Scanner scanner;
    ImageView screenCap;
    int filesize = 0;
    int i=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screen_capture);

        socket = SocketWrapper.getSocket();


        screenCap = (ImageView) findViewById(R.id.screenCap);

        new ReceiveFiles().execute();

    }

    private class ReceiveFiles extends AsyncTask<Void,Void,Void> {
        @Override
        protected Void doInBackground(Void... params) {
            while(true) {
                try {
                    is = new ObjectInputStream(socket.getInputStream());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    os = new FileOutputStream("/sdcard/"+i+".jpg");

                    byte[] sentArray = (byte[]) is.readObject();

                    os.write(sentArray);
                    os.flush();
                    publishProgress();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
                return null;
            }
        }
        @Override
        protected void onProgressUpdate(Void... values) {
            Bitmap bmp = BitmapFactory.decodeFile("/sdcard/"+i+".jpg");
            screenCap.setImageBitmap(bmp);
            i++;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_screen_capture, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:0)

无限循环不是那么无穷无尽 - 事实上,它甚至不会循环一次。

你有一个&#34;返回null&#34;在你的结尾#34;而(真实)&#34;块。第一次迭代后,您的方法将返回。