Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Google App Engine在浏览器中打开excel文件,而不是下载-Vaadin_Java_Google App Engine_Vaadin - Fatal编程技术网

Java Google App Engine在浏览器中打开excel文件,而不是下载-Vaadin

Java Google App Engine在浏览器中打开excel文件,而不是下载-Vaadin,java,google-app-engine,vaadin,Java,Google App Engine,Vaadin,我目前正在尝试创建一个web应用程序。我的任务很简单,我正在动态创建一个excel文件,并创建一个链接供客户端下载此文件。我正在使用vaadin 6.8.9和google应用程序引擎。当我在本地测试代码时,一切正常,我可以下载文件,但当我将代码部署到google app engine时,它会尝试在浏览器中打开excel文件。请帮我修一下。我尝试了所有可能的解决办法,但都没有成功 或者任何人都可以告诉我如何在不是应用程序类的vaadin类中侦听HttpServletRequestListener。

我目前正在尝试创建一个web应用程序。我的任务很简单,我正在动态创建一个excel文件,并创建一个链接供客户端下载此文件。我正在使用vaadin 6.8.9和google应用程序引擎。当我在本地测试代码时,一切正常,我可以下载文件,但当我将代码部署到google app engine时,它会尝试在浏览器中打开excel文件。请帮我修一下。我尝试了所有可能的解决办法,但都没有成功

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

下面是已部署代码的链接,

这是我的密码

            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);

尝试将内容处置标头添加到您的响应:

Content-Disposition: attachment

也许这不是答案,但你们可以在你们的浏览器中设置是否应该打开或下载一些哑剧——这也决定了会发生什么

检查HTTP响应。 1.(Ctrl+Shift+J)使用chrome->inspect->network 2.下载文件
3.使用chrome->inspect->network->file name->headers/response(如果未设置什么标头,请参阅设置了哪些标头,或将错误报告为GAE)。

将内容类型设置为
application/ocket stream
则不会查看,但通常会保存为文件。以前尝试过,它也不起作用。mimeType和targetname“\u blank”或“\u top”不会影响此问题的结果。还有其他错误,我找不到。我在“resource.getStream().setParameter(“内容处置”,“附件;filename=\”“+namefile+”\”);“如果您是指HttpServletResponse,我不能这样做。”。everytime响应返回null,因为这不是应用程序类。如果你能告诉我一个方法,这将是伟大的。这是原始代码,它不工作。我认为这是关于Google app engine的,因为这段代码在本地运行得很好。@user1763181:不幸的是,我不懂java,但你可以问一个新问题。谢谢,“新问题”是什么意思?@user1763181:在堆栈溢出上提出一个新问题,关于如何从非应用程序类的类或类似的类向内容添加标题。从第一条评论开始,就你的问题问一个新问题。