我现在从服务器上下载下来的图片有大小 但是是空白,不知道是哪里错了,各位大声帮我看看 吧!
这是服务端的代码:
public static int BUFFER_SIZE = 1024 * 1024;
static Runtime get_runtime = Runtime.getRuntime();
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
{response.setContentType("text/html;charset=GB2312");//这条语句指明了向客 户端发送的内容格式和采用的字符编码.
String filename=request.getParameter("fliename");
String loadpath=request.getSession().getServletContext().getRealPath("/")+"Image";
File file=new File(loadpath,filename);
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
//实现了一个输出流,其中的数据被写入一个 byte 数组。缓冲区会随着数据的不断写入而自动增长。可使用 toByteArray() 和 toString() 获取数据。
ByteArrayOutputStream out = new ByteArrayOutputStream(BUFFER_SIZE);
byte[] temp = new byte[BUFFER_SIZE];
int size = 0;
//读入缓冲区的字节总数,如果因为已经到达文件末尾而没有更多的数据,则返回 -1。
while ((size = in.read(temp)) != -1) {
out.write(temp, 0, size);
}
usedMemory(2);
in.close();
//Provides an output stream for sending binary data to the client.
ServletOutputStream baos = response.getOutputStream();
out.writeTo(baos);
out.flush();
客户端代码为:
try {
url = new URL(RequestURL+"?fliename="+filename);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(TIME_OUT);
conn.setRequestMethod("GET");
if(conn.getResponseCode() == HttpURLConnection.HTTP_OK){
InputStream in = conn.getInputStream();
try{
bitmap=BitmapFactory.decodeStream(in);
}catch(Exception e){
System.out.println(e);
}
in.close();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
各位大神帮我看看哪里有问题。。。谢谢了!