如何在gae中下载文件

时间:2011-12-06 12:09:53

标签: java google-app-engine download apache-poi

我不确定是否可以。 我想解析一个excel文件,让用户在我的app(gae)中下载它。

public class dumpDataServlet extends HttpServlet {  
    public void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String msg = "";
        boolean dumpFile = false;
        JSONObject json = null;
        resp.setCharacterEncoding(Constant.RESPONSE_CHARACTER_ENCODING);
        try {
            try {
                msg = req.getParameter("dumpFile") + " ";
                dumpFile = Boolean.valueOf(req.getParameter("dumpFile"));
            } catch (Exception ex) {
            }json = getFromOtherGAE();
            if (dumpFile) {
                OutputStream out = resp.getOutputStream();
                this.parseExcelFile(json).write(out); //parseExcelFile will return a HSSFWorkbook
                out.flush();
                out.close();
            } else {
                resp.getWriter().println(json.toString());
            }
        } catch (Exception ex) {
            msg = ex.getClass() + " " + ex.getMessage();
            resp.getWriter().println(msg);
        }
    }   
}

这段代码有什么问题吗?还是gae就是做不到?

谢谢。

2 个答案:

答案 0 :(得分:3)

要让浏览器将输出识别为Excel,您需要设置一些标题。类似的东西:

resp.setContentType("application/vnd.ms-excel");
resp.setHeader("Content-Disposition","attachment; filename=download.xls");

答案 1 :(得分:0)

我没有看到你初始化'json'对象。运行此代码会发生什么?