保存下载到浏览器下载而不是使用java的系统

我有一个dynamic的Web项目在我的日食,需要一个Excel数据表,并把数据库。 然后从数据库中,我创build了一个Web应用程序,供用户添加,编辑,删除和查看曾经在电子表格中的数据库中的数据。

Web应用程序将获取用户创build,编辑和删除的数据,并根据用户请求创build一个Excel电子表格。 我创build了一个写excel电子表格的链接,但是如何将新电子表格下载到用户计算机。

因此,如果他们点击链接进行下载,就会进入浏览器下载,例如他们正在使用的浏览器(例如在Chrome浏览器中),文件将转到屏幕底部的下载栏。 我一直在研究,我只是迷路了。 我正在使用java:

try{ File file = new File("C:\\eclipseworkspace\\spreatdsheet.xlsx"); if(file.delete()){ System.out.println(file.getName() + " will be overwritten, created"); }else{ System.out.println(file.getName() + " does not exist. Newly created."); } }catch(Exception e){ e.printStackTrace(); } FileOutputStream out = new FileOutputStream(newFile("C:\\eclipseworkspace\\spreatdsheet.xlsx")); workbook.write(out); out.close(); workbook.close(); System.out.println("spreadsheet .xlsx was created"); 

这是我的java代码的一部分,它导入了Apache POI类,并将其写入eclipse中的dynamicweb项目目录。

我需要什么或我需要在哪里启动,以便目录中的电子表格可以下载到用户浏览器下载,例如chrome:// downloads / in chrome browser

我想,你正在testing这个networking应用程序在同一台机器上,你看到的文件到你的下载。

您不能从Web应用程序在客户机上写任何东西。 您提供的示例代码是在服务器文件系统上写入一个文件。 不是客户。

你必须做的是把你的文件的内容写入http响应。 如果你正在使用servlets,那就好像

 public void doGet(HttpServletRequest request, HttpServletResponse response) { response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); // Instrcut the browser of what he will receive response.getOutputStream().write(/* your excel*/); } 

这样,浏览器将下载它的默认文件夹(你不能改变)