Search in sources :

Example 1 with DevPackage

use of beans.request.DevPackage in project MSEC by Tencent.

the class DownloadDevGO method doStep2.

private JsonRPCResponseBase doStep2(DevPackage request) {
    JsonRPCResponseBase response = new JsonRPCResponseBase();
    Logger logger = Logger.getLogger(DownloadDevGO.class);
    HttpSession session = getHttpRequest().getSession();
    DevPackage plan = (DevPackage) session.getAttribute("DevPackage");
    if (plan == null) {
        plan = new DevPackage();
        session.setAttribute("DevPackage", plan);
    }
    //制作tar包,这里还是同步的在当前线程完成,必要的话可以用另外一个
    //线程异步的完成。
    logger.info("begin making dev package...");
    PackDevFile packDevFile = new PackDevFile(plan, getServlet().getServletContext());
    packDevFile.run();
    if (!packDevFile.getResultString().equals("success")) {
        response.setMessage(packDevFile.getResultString());
        response.setStatus(100);
        return response;
    }
    String fileName = packDevFile.getOutputFileName();
    logger.info("dev package path:" + fileName);
    File f = new File(fileName);
    String length = String.format("%d", f.length());
    //将文件内容直接返回,注意MIME 类型,且exec函数应该返回null
    getHttpResponse().setHeader("Content-disposition", "attachment;filename=" + new File(fileName).getName());
    // set the MIME type.
    getHttpResponse().setContentType("application/x-gzip-compressed");
    getHttpResponse().setHeader("Content_Length", length);
    try {
        ServletOutputStream out = getHttpResponse().getOutputStream();
        FileInputStream fileInputStream = new FileInputStream(f);
        byte[] buf = new byte[10240];
        while (true) {
            int len = fileInputStream.read(buf);
            if (len <= 0) {
                break;
            }
            out.write(buf, 0, len);
        }
        out.flush();
        out.close();
        fileInputStream.close();
        f.delete();
        return null;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
Also used : JsonRPCResponseBase(ngse.org.JsonRPCResponseBase) ServletOutputStream(javax.servlet.ServletOutputStream) HttpSession(javax.servlet.http.HttpSession) DevPackage(beans.request.DevPackage) Logger(org.apache.log4j.Logger) File(java.io.File) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException)

Aggregations

DevPackage (beans.request.DevPackage)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 HttpSession (javax.servlet.http.HttpSession)1 JsonRPCResponseBase (ngse.org.JsonRPCResponseBase)1 Logger (org.apache.log4j.Logger)1