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.