使用TOMCAT时,如何将文件保存在客户端PC上,而不是服务器PC上



我有一个使用POI保存XLSX的服务,当我将文件保存到服务器上的路径上时,它将保存在服务器上的PC上。

我程序中的部分代码:

public static final String EXCELPATH = "C:\SAMPLE\REPORTS\";
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("Sheet 1");
filepath = EXCELPATH + "TCRKBOS_050020_" + mTodayDate;
FileOutputStream fileOut = new FileOutputStream(filepath + ".csv");
Row row;
Cell cell;
// ********* SAMPLE CELL **************** //
row = sheet.createRow(0);
cell = row.createCell(0);
cell.setCellValue("REPDTE");
cell.setCellStyle(centerHeader1);
cell = row.createCell(1);
cell.setCellValue("BNKCDE");
cell.setCellStyle(centerHeader1);
conn.close();
wb.write(fileOut);
fileOut.flush();
fileOut.close();

您无法在客户端PC上保存文件。这是管理是否在客户端PC上下载文件的浏览器。

您可以做的是通过HTTP作为响应发送文件。我假设您在这里使用servlet。在您的Servlet内部,如果您希望您的文件下载以响应GET请求,则可以执行此类操作:

  protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
      throws ServletException, IOException {
        resp.setContentType("text/csv");
        resp.setHeader("Content-disposition", "attachment; filename=TCRKBOS_050020_" + mTodayDate + ".csv");
        try (OutputStream out = resp.getOutputStream()) {
            //todo: write the CSV data to the output stream
        }
    }

最新更新