高分散贴在线等待

wang_peng1 2008-11-26 09:01:45
我在客户端用DataOutputStream out = conn.openDataOutputStream();
System.out.println("datalen="+data.length);
out.writeInt(data.length);
out.write(data, 0, data.length);//发送数据至服务器
out.flush();
out.close();
data是存储图片格式的字节数组

服务器端
DataInputStream in = new DataInputStream(request.getInputStream());

byte[] b = new byte[in.readInt()];
int len =in.read(b);
// System.out.println("@@"+ len);

File f = new java.io.File("c:/" + System.currentTimeMillis() + ".png");
DataOutputStream o = new DataOutputStream(new FileOutputStream(f));

o.write(b, 0, len);
o.flush();
o.close();
为什么我接收的图片 只能看见1/3差不多 其它部分看不清
...全文
83 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
tiyuzhongxin789 2008-11-28
  • 打赏
  • 举报
回复
服务器端改成这样试试
DataInputStream in = new DataInputStream(request.getInputStream());
int len=in.readInt();
byte[] b = new byte[len];
in.read(b);
System.out.println("@@"+ len);

File f = new java.io.File("c:/" + System.currentTimeMillis() + ".png");
DataOutputStream o = new DataOutputStream(new FileOutputStream(f));

o.write(b, 0, len);
o.flush();
o.close();

还有你注意看下生成的图片文件大小是否与之前的一致
moren123 2008-11-28
  • 打赏
  • 举报
回复
加上延时看看。
filec75 2008-11-27
  • 打赏
  • 举报
回复
不懂,up
homesos 2008-11-27
  • 打赏
  • 举报
回复
1.看发送端与接收端所收到的数据长度是否一致。
2.如果图片太大还是分包发,不要一次性发,发送包的尺寸是有限制的。
kf156 2008-11-26
  • 打赏
  • 举报
回复
服务器端改成这样试试
DataInputStream in = new DataInputStream(request.getInputStream());
int len=in.readInt();
byte[] b = new byte[len];
in.read(b);
System.out.println("@@"+ len);

File f = new java.io.File("c:/" + System.currentTimeMillis() + ".png");
DataOutputStream o = new DataOutputStream(new FileOutputStream(f));

o.write(b, 0, len);
o.flush();
o.close();

还有你注意看下生成的图片文件大小是否与之前的一致


  • 打赏
  • 举报
回复
很可能是发的时候,数据没有传完你就flush了,在flush之前要判断一下
yanhan0615 2008-11-26
  • 打赏
  • 举报
回复
客户端向服务器POST的时候注意加上Content-Length
上善_若水 2008-11-26
  • 打赏
  • 举报
回复
看你的代码比较晕,为什么要一次把所有的字节读取完毕,要是文件很大的话,直接就超出内存范围了,
不过图片还是比较难超的,提个醒而已。

DataInputStream in = new DataInputStream(new FileInputStream("path"));
//DataInputStream in = new DataInputStream(request.getInputStream());

File f = new java.io.File("c:/" + System.currentTimeMillis() + ".jpg");
DataOutputStream o = new DataOutputStream(new FileOutputStream(f));
int len = 0;
byte[] b = new byte[1024];
while ((len = in.read(b)) != -1) {
o.write(b, 0, len);
}
o.flush();
o.close();


以上代码把path改成相应的路径测试通过,只要保证你的request.getInputStream()是正确的就OK了。

13,100

社区成员

发帖
与我相关
我的任务
社区描述
Java J2ME
社区管理员
  • J2ME社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧