这是控制器类@Controllerpublic class ReportController { @RequestMapping("/reports.htm") public void onSubmit(ModelMap map, HttpServletResponse response) { PayrollService ps = new PayrollServiceImpl(); JRBeanCollectionDataSource jr = new JRBeanCollectionDataSource(ps.listAllLoans(), false); try { JasperPrint jp = JasperFillManager.fillReport(new FileInputStream("C:\\Documents and Settings\\Administrator\\workspace\\payroll\\WebContent\\WEB-INF\\payrollReports\\report2.jasper"), null, jr); JRExporter jre = new JRPdfExporter(); jre.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "report.pdf"); jre.setParameter(JRExporterParameter.JASPER_PRINT, jp); ServletOutputStream output = response.getOutputStream(); jre.setParameter(JRExporterParameter.OUTPUT_STREAM, output); jre.exportReport(); output.close(); } catch (Exception e) { e.printStackTrace(); } }}当我运行此文件时,将打开一个对话框,要求下载文件reports.htm,当我打开文件时,我得到了%PDF-1.4 %âãÏÓ 3 0 obj <>stream xœ+är á26S°00SIár á ä2Rð‰!ˆ´01RÉåÒw3T0²Ò¸44C² @ jJ的5Z〜[ë™!t€tååÒ¹4¼‹R ‹@(¤c1,:H§€ƒCssC3C endstream endobj 1 0 obj /父对象4 0 R /目录3 0 R /类型/页面/资源 /字体>> / MediaBox [0 0595842] >> endobj 5 0 obj [1 0 R / XYZ [0 854 0] endobj 2 0 obj endobj 4 0 obj endobj 6 0 obj endobj 7 0 obj endobj 8 0 obj /页4 0 R >> endobj 9 0 obj endobj外部参照0 10 0000000000 65535 f 0000000220 00000 n 0000000487 00000 n 0000000015 00000 n 0000000574 00000 n 0000000453 00000 n 0000000624 00000 n 0000000677 00000 n 0000000708 00000 n 0000000810 00000 n 预告片 ] /信息 9 0 R /尺寸10 >> startxref 976 %% EOF但是,当我保存文件并使用Adobe PDF打开时,我得到了正确的PDF。如何下载Reports.pdf而不是reports.htm? (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 设置Content-Disposition和Content-Type标头,以指示浏览器如何处理资源。response.setContentType("application/pdf");response.setHeader("Content-Disposition", "attachment; filename=\"report.pdf\""); (adsbygoogle = window.adsbygoogle || []).push({}); 10-04 21:11