0

我目前正在尝试创建一个 Web 应用程序。我的任务很简单,我正在动态创建一个 excel 文件并创建一个链接供客户端下载该文件。我正在使用 vaadin 6.8.9 和谷歌应用引擎。当我在本地测试代码时,一切正常,我可以下载文件,但是当我将代码部署到谷歌应用引擎时,它会尝试在浏览器中打开 excel 文件。请帮我修复它。我尝试了所有可能的解决方案,但没有奏效。

或者任何人都可以告诉我如何在不是应用程序类的 vaadin 类中监听 HttpServletRequestListener。并设置其 Content-disposition 并写入文件。

这是部署代码的链接, http: //vaadingo.appspot.com/

这是我的代码,

            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            WritableWorkbook workBook;
            try {
                workBook = Workbook.createWorkbook(outputStream);
                WritableSheet sheet = workBook.createSheet("User List", 0);

                // Generates Headers Cells
                WritableFont headerFont = new WritableFont(WritableFont.TAHOMA, 12, WritableFont.BOLD);
                WritableCellFormat headerCellFormat = new WritableCellFormat(headerFont);
                try {
                    headerCellFormat.setBackground(Colour.PALE_BLUE);
                } catch (WriteException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                try {
                    sheet.addCell(new jxl.write.Label(1, 1, "LastName", headerCellFormat));
                    sheet.addCell(new jxl.write.Label(2, 1, "FirstName", headerCellFormat));
                    WritableFont dataFont = new WritableFont(WritableFont.TAHOMA, 12);
                    WritableCellFormat dataCellFormat = new WritableCellFormat(dataFont);
                    sheet.addCell(new jxl.write.Label(1, 2, "last", dataCellFormat));
                    sheet.addCell(new jxl.write.Label(2, 2, "first", dataCellFormat));
                    try {
                        workBook.write();
                        workBook.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                } catch (RowsExceededException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (WriteException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                final ByteArrayInputStream in = new ByteArrayInputStream(outputStream.toByteArray());


                 StreamSource source = new StreamSource() {

                    public InputStream getStream() {
                        // TODO Auto-generated method stub

                        return in;
                    }
                };

                String namefile = "deneme.xls";
                StreamResource resource = new StreamResource(source, namefile, getApplication());

                resource.getStream().setParameter("Content-Disposition", "attachment; filename=\"" + namefile + "\"");
                resource.setMIMEType("application/vnd.ms-excel");
                resource.setCacheTime(-1);
                Link link = new Link();
                link.setCaption("dosya");
                link.setResource(resource);
                link.setTargetName("_blank");
                mainLayout.addComponent(link);
4

2 回答 2

2

尝试在响应中添加 content-disposition 标头:

Content-Disposition: attachment
于 2013-06-09T13:41:50.377 回答
0

也许这不是答案,但您可以在浏览器中设置是否应该打开或下载某些 mime - 它也决定了会发生什么。

检查 HTTP 响应。1. (Ctrl+Shift+J) 使用 chrome -> 检查 -> 网络 2. 下载文件 3. 使用 chrome -> 检查 -> 网络 -> 文件名 -> 标题/响应(如果未设置,请查看设置了哪些标题尝试解决它或向 GAE 报告错误)。

于 2013-07-03T06:59:21.427 回答