Java服务器无效异常。插座

时间:2015-03-17 17:24:53

标签: java image sockets chat server

我一直在修理我的服务器很多次,现在我又被卡住了。

所以我一直在尝试通过我的clientGUI将图片发送到服务器,因此该人可以看到即时发送的文件。我真的不知道如何解释这个。但无论如何。我只是试图发送一个文件,以便其他客户端可以接受并保存它。但是当我按下按钮图片时(它应该自动将图片发送到我放置的桌面)它确实有效,

  

接受连接; localhost / 127.0.0.1 - 1500   发送C:/Users/Barry/Desktop/Ceo/Cao6.jpg(15306 bytes)   服务器已关闭连接

但是我在桌面和ServerGUI中没有得到任何东西,我可以看到它说:

Exception reading Streams: java.io.StreamCorruptedException: invalid type code: FF

我不知道如何修复它,我需要一些想法如何解决这个问题。

Sendpic.java

public void SendPic() throws IOException {
    JFileChooser chooser = new JFileChooser();
    String FILESEND = "C:/Users/Barry/Desktop/Ceo/Cao6.jpg";
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    OutputStream os = null;

    try {

        // send file

        File myFile = new File (FILESEND);
        byte [] mybytearray  = new byte [(int)myFile.length()];
        fis = new FileInputStream(myFile);
        bis = new BufferedInputStream(fis);
        bis.read(mybytearray,0,mybytearray.length);
        os = socket.getOutputStream();
        System.out.println("Sending " + FILESEND + "(" + mybytearray.length + " bytes)");
        os.write(mybytearray,0,mybytearray.length);
    } catch (IOException e) {

        e.printStackTrace();
    }

}

getPic.Java

     final static int FILE_SIZE = 6022386;
public void getPic() throws IOException {
    JFileChooser chooser = new JFileChooser();
    String FILETORECEIVED = "C:/Users/Barry/Desktop/";

     int bytesRead;
     int current = 0;

    FileOutputStream fos = null;
        BufferedOutputStream bos = null;
    try {

    byte [] mybytearray  = new byte [FILE_SIZE];
      InputStream is = socket.getInputStream();
      fos = new FileOutputStream(FILETORECEIVED);
      bos = new BufferedOutputStream(fos);
      bytesRead = is.read(mybytearray,0,mybytearray.length);
      current = bytesRead;

      do {
         bytesRead =
            is.read(mybytearray, current, (mybytearray.length-current));
         if(bytesRead >= 0) current += bytesRead;
      } while(bytesRead > -1);

      bos.write(mybytearray, 0 , current);
      bos.flush();
      System.out.println("File " + FILE_SIZE
          + " downloaded (" + current + " bytes read)");
}

    finally {
      if (fos != null) fos.close();
      if (bos != null) bos.close();

    }
  }

ActionPerformed `

public void actionPerformed(ActionEvent e) {
        Object button = e.getSource();


        if (button == btnPicture) {
            try {
                controller.SendPic();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            return;
        }`

0 个答案:

没有答案