|
使用http传图片时,真机(包括2.2包刷的平板还有htc的一部2.2的手机)有时能读到图片有时读不到(读不到的概率大于读的到,但貌似排除图片大过头了的情况下,越大的图片越容易读到),而且服务器端和客户端都不报错,我打印过服务端从数据库取到的byte[],总是能取到的,但就是不知道为什么传到客户端时有事就是空的。另外在模拟器上就总是能读到图片,试了N次没出现过读到空的,请问这是什么原因造成的。
//服务端代码
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();
}
}
}
[/code]//获取图片
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("OST");
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;
}
[/code] |
|