【问题标题】:GWT: Trying to save a file in server sideGWT:尝试在服务器端保存文件
【发布时间】:2014-09-13 12:08:16
【问题描述】:

我的服务器端有这个方法,可以在我的war文件夹中保存一个新的excel文件。

但是当我从我的 tomcat 运行相同的代码时,它不会将文件保存在任何地方,

我希望它将文件保存在我的 webapps 项目文件夹中 这里:

E:\tomcat 7\apache-tomcat-7.0.54\webapps\foodchain

但它没有保存在那里..

请指导我哪里错了。 在 localhost 上运行时文件的保存位置。

代码:

    public String exportToExcel(ArrayList<ExcelDataDTO> excelDataList) {
    try {
        FileOutputStream fileOut = new FileOutputStream(".\\images\\report.xls");//"D:\\POI111.xls"
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet worksheet = workbook.createSheet("Orders Worksheet");
        for(int i=0; i< excelDataList.size(); i ++){
        HSSFRow row1 = worksheet.createRow((short) 0);
        row1.createCell((short) 0).setCellValue(excelDataList.get(i).getChain());
        row1.createCell((short) 1).setCellValue(excelDataList.get(i).getOrder());
        row1.createCell((short) 2).setCellValue(excelDataList.get(i).getShop());
        row1.createCell((short) 3).setCellValue(excelDataList.get(i).getCustomerAddress());
        row1.createCell((short) 4).setCellValue(excelDataList.get(i).getCar());
        row1.createCell((short) 5).setCellValue(excelDataList.get(i).getLoaded());
        }
        workbook.write(fileOut);
        fileOut.flush();
        fileOut.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "exported";
}

【问题讨论】:

  • 您是否查看过 Tomcat 工作目录?它应该在$TOMCAT_HOME/work/Catalina/
  • 谢谢,我在看这里:E:\tomcat 7\apache-tomcat-7.0.54\work\Catalina\localhost\foodchain.... 但它不在这里

标签: tomcat


【解决方案1】:

“当前”目录可能因环境而异,因此您不应依赖它。在 servlet 方法中,您可以使用此代码来检索 webapp 根目录的绝对文件路径:

String rootDir = getServletContext().getRealPath("/");

在你的情况下它应该返回:E:\tomcat 7\apache-tomcat-7.0.54\webapps\foodchain\

用它来构建xls文件的路径,你应该很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多