2015-05-28 72 views
0

同事。 我正在做大學評估,我需要實現FTP服務器通信。序列化似乎可以正常工作,但反序列化不會。 的步驟我做:StreamCorruptedException從ftp反序列化對象

  1. 檢索FTP文件流ByteArrayOutputStream
  2. 關閉流
  3. 寫流字節數組ByteArrayInputStream的包裹進ObjectInputStream中
  4. 讀取的字節數組。

當我嘗試讀取對象,我得到

java.io.StreamCorruptedException: invalid type code: 00. 

下面的代碼:

try { 
     ftpClient = new FTPClient(); 
     ftpClient.connect("********",21); 
     ftpClient.login("******", "******"); 
     ftpClient.setFileTransferMode(ftpClient.BINARY_FILE_TYPE); 
     ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
     boolean ok = ftpClient.retrieveFile("/data/" + fileName,bos); 
     System.out.println("" + ok + " " + ftpClient.getReplyCode()); 
     bos.close(); 
     byte[] b = bos.toByteArray(); 
     ois = new ObjectInputStream(new ByteArrayInputStream(b)); 
     if (fileName.contains("dataW")) { 
      Warrior character = (Warrior)ois.readObject(); 
      System.out.println(ftpClient.getReplyCode()); 
      MainMenu frame = new MainMenu(character); 
      frame.setVisible(true); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      parent.setVisible(false); 
     } else { 
      Rogue character = (Rogue)ois.readObject(); 
      MainMenu frame = new MainMenu(character); 
      frame.setVisible(true); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      parent.setVisible(false); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (ClassNotFoundException e) { 
     e.printStackTrace(); 
    } 
} 

堆棧跟蹤:

java.io.StreamCorruptedException: invalid type code: 00 
at java.io.ObjectInputStream.readObject0(Unknown Source) 
at java.io.ObjectInputStream.defaultReadFields(Unknown Source) 
at java.io.ObjectInputStream.readSerialData(Unknown Source) 
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source) 
at java.io.ObjectInputStream.readObject0(Unknown Source) 
at java.io.ObjectInputStream.readObject(Unknown Source) 
at com.mif.mobr0858.GUI.LoadThread.run(LoadThread.java:47) 
at com.mif.mobr0858.GUI.FileChooser$2.actionPerformed(FileChooser.java:77) 
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) 
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) 
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) 
at javax.swing.DefaultButtonModel.setPressed(Unknown Source) 
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) 
at java.awt.Component.processMouseEvent(Unknown Source) 
at javax.swing.JComponent.processMouseEvent(Unknown Source) 
at java.awt.Component.processEvent(Unknown Source) 
at java.awt.Container.processEvent(Unknown Source) 
at java.awt.Component.dispatchEventImpl(Unknown Source) 
at java.awt.Container.dispatchEventImpl(Unknown Source) 
at java.awt.Component.dispatchEvent(Unknown Source) 
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) 
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) 
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) 
at java.awt.Container.dispatchEventImpl(Unknown Source) 
at java.awt.Window.dispatchEventImpl(Unknown Source) 
at java.awt.Component.dispatchEvent(Unknown Source) 
at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
at java.awt.EventQueue.access$400(Unknown Source) 
at java.awt.EventQueue$3.run(Unknown Source) 
at java.awt.EventQueue$3.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
at java.awt.EventQueue$4.run(Unknown Source) 
at java.awt.EventQueue$4.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
at java.awt.EventQueue.dispatchEvent(Unknown Source) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
at java.awt.WaitDispatchSupport$2.run(Unknown Source) 
at java.awt.WaitDispatchSupport$4.run(Unknown Source) 
at java.awt.WaitDispatchSupport$4.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.awt.WaitDispatchSupport.enter(Unknown Source) 
at java.awt.Dialog.show(Unknown Source) 
at java.awt.Component.show(Unknown Source) 
at java.awt.Component.setVisible(Unknown Source) 
at java.awt.Window.setVisible(Unknown Source) 
at java.awt.Dialog.setVisible(Unknown Source) 
at com.mif.mobr0858.GUI.WelcomeFrame$2.actionPerformed(WelcomeFrame.java:45) 
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) 
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) 
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) 
at javax.swing.DefaultButtonModel.setPressed(Unknown Source) 
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) 
at java.awt.Component.processMouseEvent(Unknown Source) 
at javax.swing.JComponent.processMouseEvent(Unknown Source) 
at java.awt.Component.processEvent(Unknown Source) 
at java.awt.Container.processEvent(Unknown Source) 
at java.awt.Component.dispatchEventImpl(Unknown Source) 
at java.awt.Container.dispatchEventImpl(Unknown Source) 
at java.awt.Component.dispatchEvent(Unknown Source) 
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) 
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) 
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) 
at java.awt.Container.dispatchEventImpl(Unknown Source) 
at java.awt.Window.dispatchEventImpl(Unknown Source) 
at java.awt.Component.dispatchEvent(Unknown Source) 
at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
at java.awt.EventQueue.access$400(Unknown Source) 
at java.awt.EventQueue$3.run(Unknown Source) 
at java.awt.EventQueue$3.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
at java.awt.EventQueue$4.run(Unknown Source) 
at java.awt.EventQueue$4.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
at java.awt.EventQueue.dispatchEvent(Unknown Source) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.run(Unknown Source) 

連載:

try { 
     //get current date 
     Date date = new Date(); 
     DateFormat dateFormat = new SimpleDateFormat("dd-MM_HH.mm"); 
     String fileName; 
     String fileName1; 
     //select appropriate file name 
     if (character instanceof Warrior) { 
      fileName = "dataW_" + dateFormat.format(date).toString() + ".dat"; 
      fileName1 = "dataBW_" + dateFormat.format(date).toString() + ".dat"; 
     } else { 
      fileName = "dataR_" + dateFormat.format(date).toString() + ".dat"; 
      fileName1 = "dataRW_" + dateFormat.format(date).toString() + ".dat"; 
     } 
     //create file and write data 
     File f = new File(fileName); 
     File f1 = new File(fileName1); 
     oos = new ObjectOutputStream(new FileOutputStream(f)); 
     oos.writeObject(character); 
     oos.close(); 
     oos = new ObjectOutputStream(new FileOutputStream(f1)); 
     oos.writeObject(frame); 
     //connect to FTP 
     FTPClient ftpClient = new FTPClient(); 
     ftpClient.connect("*****", 21); 
     boolean connOK = ftpClient.login("*****", "****"); 
     if (connOK) { 
      MainMenu.uploadSingleFile(ftpClient, f.getAbsolutePath(), "data/" + fileName); 
     } 
     } catch(IOException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       oos.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

上傳代碼:

public static boolean uploadSingleFile(FTPClient ftpClient, String localFilePath, 
     String remoteFilePath) throws IOException { 
    File localFile = new File(localFilePath); 

    InputStream inputStream = new FileInputStream(localFile); 
    try { 
      ftpClient.setFileTransferMode(ftpClient.BINARY_FILE_TYPE); 
      ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE); 
      return ftpClient.storeFile(remoteFilePath, inputStream); 
    } finally { 
     inputStream.close(); 
    } 

我將不勝感激,如果有人可以幫助我解決問題

+0

序列化代碼在哪裏? – EJP

+0

加入問題 –

回答

0

您還沒有關閉第一ObjectOutputStream.的文件不完整。

+1

對OP的說明 - 也可能存在其他問題。例如,在您沒有向我們顯示的上傳代碼中。 –

+0

如不使用二進制模式進行上傳。 – EJP

+0

是不是在關閉ObjectOutputStream在finally塊中不夠? –