当我尝试将.csv文件从远程服务器下载到本地浏览器时找不到文件

时间:2018-04-04 13:02:30

标签: java html servlets download remote-server

我有一台带有Tomcat 9的远程Linux服务器(Debian 9.2)。我在服务器上加载了一个生成.csv文件的Web应用程序,用户可以连接到服务器并下载它。 当程序在localhost上运行时,它运行良好,但是,当它在远程服务器上运行时,浏览器会说:file not found

这是我的代码:

private void writeFile(String nomeFile, String content, HttpServletResponse response) throws IOException {

     response.setContentType("text/csv");
     response.setHeader("Content-disposition","attachment; filename="+nomeFile);


    String filename=nomeFile;
     try {
     File file = new File(filename);


     FileWriter fw = new FileWriter(file);
     BufferedWriter bw = new BufferedWriter(fw);
     bw.write(content);
     bw.flush();
     bw.close();
     }
     catch(IOException e) {
     e.printStackTrace();
     }


     // This should send the file to browser
     ServletOutputStream out = response.getOutputStream();

    FileInputStream in = new FileInputStream(filename);
     byte[] buffer = new byte[4096];
     int length;
     while ((length = in.read(buffer)) > 0){
        out.write(buffer, 0, length);
     }
     in.close();
     out.flush();



}

我正在尝试调试它,但我不知道错误可能在哪里。实现代码的servlet在localhost上运行良好。为什么它在远程服务器上失败?

2 个答案:

答案 0 :(得分:0)

如果您在本地和远程计算机上使用相同的代码并且它在一台机器上工作而在另一台机器上不起作用则表示以下两种情况之一:

1)Tomcat的localhost配置可能与远程主机不同      - 这可能会导致Tomcat搜索“不同”文件夹中的文件等。或者 2)文件仅存在于localhost,并且不存在于远程主机上。

答案 1 :(得分:0)

您可以在DevTools中看到您的浏览器在网络选项卡上的什么是请求字符串到远程服务器获取文件并检查它。 还要检查远程服务器上的上下文。部署项目时,您的localhost可能会有所不同。 键入放置csv文件和路径的位置。

相关问题