quick-cocos2d-x下载文件

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

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脚本:

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时,服务器将文件数据发送到客户端。

你可能感兴趣的:(下载,quick-cocos2dx)