Re: [問題] DataOutput傳輸檔案的問題

看板java作者 (sbr)時間16年前 (2009/09/13 18:32), 編輯推噓2(204)
留言6則, 3人參與, 最新討論串4/4 (看更多)
※ 引述《reon (完成這條天堂路)》之銘言: : : //--------------傳送端------------------------------------- : : byte[] buffer=new byte[512]; : : int i; : : dos=new DataOutputStream(conn.openDataOutputStream()); : 改成ObjectOutputStream oos = new ObjectOutputStream( con..自行完成 ); MIDP 沒有 ObjectInputStream/ObjectOutputStream,因為他不支援 serialization。 : : FileInputStream fis=new FileInputStream(new File(FilePath)); //C://1.jpg : 這行留著 : : while((i=fis.read(buffer))!=-1) : : dos.write(buffer,0,i); : : fis.close(); : : dos.close(); : 這段整個改掉 也不用宣告buffer陣列大小了 : BufferedInputStream fin = new BufferedInputStream(fis); : ByteArrayOutputStream bout = new ByteArrayOutputStream(); : byte[] input = new byte[1]; : while(fin.read(input) !=-1){ : bout.write(input); : } : input = bout.toByteArray(); : oos.write(input); : oos.flush(); : oos.close(); 這樣子更糟。 InputStream - read(byte[]) 不見得每次都會把指定的 byte array buffer 填滿, 你一味把 byte array 全數收集在 ByteArrayOutputStream 會有垃圾 bytes 在裡頭 (也就是說 ByteArrayOutputStream 收集的 bytes 可能會比 input stream 的 bytes 還多) 雖然上面的問題在 buffer size 為 1 時就不成立,但是總的來說不偵測 read 操作 實際讀進多少 bytes 就把 buffer 全數做後續處理是不正確的。 : 這是我碩論時在寫網路程式的遇到類似的問題 希望可以解決你的問題 : 因為DataOutputStream在輸出的時候好像會加料= =|| : 好像是Header還是啥的問題 輸出字串和字元是還不太有問題 : 不過小到像byte的資料似乎會有點問題 你記錯了。ObjectOutputStream 才會加料(stream header),DataOutputStream 則不會,他是方便用來傳輸 primitive 型別數據,讓你不需要自己搞 byte level 的事。 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 218.173.136.1 ※ 編輯: sbrhsieh 來自: 218.173.136.1 (09/13 18:33) ※ 編輯: sbrhsieh 來自: 218.173.136.1 (09/13 18:34)

09/13 19:18, , 1F
這篇看起來比較正確 XD
09/13 19:18, 1F
※ 編輯: sbrhsieh 來自: 218.173.136.1 (09/13 20:05)

09/13 22:28, , 2F
ByteArrayXXXXXStream這方法是讀取/寫入 金鑰的方式
09/13 22:28, 2F

09/13 22:30, , 3F
基本上不會有問題吧 那時候研究很久才從Java網站找到的
09/13 22:30, 3F

09/13 22:33, , 4F
因為金鑰只要錯一個bit整個 就無法解密
09/13 22:33, 4F

09/13 22:37, , 5F
所以應該是不會有所謂的多餘的東西出來吧@@
09/13 22:37, 5F

09/13 23:07, , 6F
對,在 buffer size=1 時不會有問題(文中有提到)
09/13 23:07, 6F
文章代碼(AID): #1AhCf2Jk (java)
文章代碼(AID): #1AhCf2Jk (java)