2012-03-23 52 views
-2

我有一个项目,我正在处理宁静的web服务,尤其是需要返回android客户端的图像时,当客户端进入“画廊”,他需要得到一个根集合,必须运行服务器上的静态文件夹中的所有文件夹和文件(图像)。有人可以帮忙吗?如何返回客户可以访问的图像以获取详细视图?java restful web service image gallery

+1

你问一个非常普遍的问题。谷歌一些,并找到你想要的一些教程,然后尝试一些你自己的东西。一旦你有一个特定的问题,回来问一下。 – Jim 2012-03-23 16:04:05

回答

0

你只需要getHttpServletRequest和HttpServletResponse对象的类型和应用下面的代码 -

 String filePath = request.getParameter(YOUR_FILE_PATH_PARAMETER"); 
     String filePath = filePath; 
     response.setContentType("application/octet-stream"); 
     response.setHeader("Content-Disposition", "attachment;filename=" 
       + "YOUR_FILE_NAME"); 

     // Get it from file system 
     FileInputStream in = new FileInputStream(new File(filePath)); 


     ServletOutputStream out = response.getOutputStream(); 

     byte[] outputByte = new byte[4096]; 
     // copy binary content to output stream 
     while (in.read(outputByte, 0, 4096) != -1) { 
      out.write(outputByte, 0, 4096); 

         in.close(); 
         out.flush(); 
         out.close();