我不确定是否可以。 我想解析一个 Excel 文件并让用户在我的应用程序(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就是做不到?
谢谢。
最佳答案
为了让浏览器将输出识别为 Excel,您需要设置一些标题。像这样的东西:
resp.setContentType("application/vnd.ms-excel");
resp.setHeader("Content-Disposition","attachment; filename=download.xls");
关于java - 如何在gae中下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8399650/