u010416722 2014-12-03 08:45 采纳率: 0%
浏览 5183

Android客户端从服务器下载图片,服务器端是如何实现的

我现在从服务器上下载下来的图片有大小 但是是空白,不知道是哪里错了,各位大声帮我看看 吧!
这是服务端的代码:
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();
    }  


    各位大神帮我看看哪里有问题。。。谢谢了!
  • 写回答

3条回答 默认 最新

  • Captain_CoCo_X 2014-12-03 11:46
    关注

    1、首先要确定客户端是否完整接收图片数据了(可以先保存到手机sd卡上,查看是否能下载);
    2、如果是将下载的图片加载进android,数据加载正确了,再看看是不是UI线程是否更新。

    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试