Re: [問題] DataOutput傳輸檔案的問題
※ 引述《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
09/13 19:18, 1F
※ 編輯: sbrhsieh 來自: 218.173.136.1 (09/13 20:05)
推
09/13 22:28, , 2F
09/13 22:28, 2F
→
09/13 22:30, , 3F
09/13 22:30, 3F
→
09/13 22:33, , 4F
09/13 22:33, 4F
→
09/13 22:37, , 5F
09/13 22:37, 5F
→
09/13 23:07, , 6F
09/13 23:07, 6F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 4 之 4 篇):