Quick-cocos2d-x download files

Source: Internet
Author: User

The server is still a servlet and the code is as follows:


[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 ();
}

}
The following is the client lua script:


[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 ()
Note: http: //.../download is the response url of the servlet above. When Lua accesses this url, the server sends the file data to the client.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.