当前位置:编程学习 > wap >>

quick-cocos2d-x下载文件

服务器端仍然是一个servlet,代码如下:


[java]  public class DownServlet extends HttpServlet { 
 
    private static final long serialVersionUID = 1L; 
 
    @Override 
    protected void service(HttpServletRequest request, 
            HttpServletResponse response) throws ServletException, IOException { 
        String path = "D:/Downloads/LuaXML_101012.zip"; 
        File file = new File(path); 
        InputStream is = new BufferedInputStream(new FileInputStream(file)); 
        byte[] buffer = new byte[is.available()]; 
        is.read(buffer); 
        is.close(); 
        response.reset(); 
        response.addHeader("Content-Disposition", "attachment;filename=" 
                + new String(file.getName().getBytes("utf-8"), "ISO-8859-1")); 
        response.addHeader("Content-Length", "" + file.length()); 
        OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); 
        response.setContentType("application/octet-stream"); 
        toClient.write(buffer); 
        toClient.flush(); 
        toClient.close(); 
    } 
 

public class DownServlet extends HttpServlet {

 private static final long serialVersionUID = 1L;

 @Override
 protected void service(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {
  String path = "D:/Downloads/LuaXML_101012.zip";
  File file = new File(path);
  InputStream is = new BufferedInputStream(new FileInputStream(file));
  byte[] buffer = new byte[is.available()];
  is.read(buffer);
  is.close();
  response.reset();
  response.addHeader("Content-Disposition", "attachment;filename="
    + new String(file.getName().getBytes("utf-8"), "ISO-8859-1"));
  response.addHeader("Content-Length", "" + file.length());
  OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
        response.setContentType("application/octet-stream");
        toClient.write(buffer);
        toClient.flush();
        toClient.close();
 }

}
下面是客户端lua脚本:


[ruby]  local function callback1(event) 
    local ok = (event.name == "completed") 
    local request = event.request 
    request:saveResponseData("a.zip") 
     
end 
 
local request1 = network.createHTTPRequest(callback1, "http://localhost:8080/download", "POST") 
request1:start() 

local function callback1(event)
 local ok = (event.name == "completed")
 local request = event.request
 request:saveResponseData("a.zip")
 
end

local request1 = network.createHTTPRequest(callback1, "http://localhost:8080/download", "POST")
request1:start()
说明:http://....../download是上面servlet的响应url,当Lua访问此url时,服务器将文件数据发送到客户端。

补充:移动开发 , 其他 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,