http传图片,真机有时能读到有时读不到

momolvp 2011-06-08 02:58:57
使用http传图片时,真机(包括2.2包刷的平板还有htc的一部2.2的手机)有时能读到图片有时读不到(读不到的概率大于读的到,但貌似排除图片大过头了的情况下,越大的图片越容易读到),而且服务器端和客户端都不报错。但是在模拟器上就总是能读到图片,请问这是什么原因造成的。

//服务端代码
public void map() {
ServletOutputStream out = null;
int mapId = 0;
byte[] is = null;
try {
String content = IOUtils.toString(getRequest().getInputStream());
JSONObject json = new JSONObject(content);
mapId = json.getInt("mapId");
} catch (Exception e) {
log.error("读取参数错误", e);
return;
}

try {
SystemInterface msi = SystemInterface.getInstance();
// 从数据库读取图品的io流并转为byte[]
is = msi.mapMap(mapId);
} catch (Exception e) {
log.error("读取失败", e);
return;
}

if(is != null){
try {
out = getResponse().getOutputStream();
out.write(is);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}

}

}

//获取图片
public static Bitmap getImageResult(String actionUrl, String request,int mapSize) {
Bitmap bm = null;
try {
URL url = new URL("http://" + host + ":" + port + actionUrl);
Log.d(TAG, String.format("Request (url: %s; Extra: %s)", url.toString(), request));

HttpURLConnection connect = (HttpURLConnection) url.openConnection();
connect.setDoOutput(true);
connect.setDoInput(true);
connect.setRequestMethod("POST");
connect.setRequestProperty("Content-Type", "text/html");
connect.setRequestProperty("Accept", "*/*");
connect.setRequestProperty("Connection", "keep-alive");
connect.setConnectTimeout(15 * 1000);
connect.setRequestProperty("Accept-Language", "en-us");
connect.setRequestProperty("Accept-Encoding", "gzip, deflate");

OutputStreamWriter wr = new OutputStreamWriter(connect.getOutputStream());
wr.write(request);
wr.flush();
InputStream is = connect.getInputStream();
//图片
if(is != null){
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
options.inSampleSize = mapSize;
bm = BitmapFactory.decodeStream(is, null, options);
}
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return bm;
}


...全文
89 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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