qq_32929101 2017-11-29 08:23 采纳率: 80%
浏览 3055
已采纳

java代码实现pdf文档下载

楼主试了半天,想用controller直接实现来着,但一直都得不到文件

@Controller
@RequestMapping("/reportDownload")
public class ReportDownloadController extends BaseController{

private static Logger logger=Logger.getLogger(ReportDownloadController.class);

@RequestMapping("/downReport")
public HttpServletResponse busReport(HttpServletRequest request,BigDecimal modelId,BigDecimal planId,HttpServletResponse response) throws IOException {
    UtilReport util = new UtilReport();
    String fileDownloadName = null;
    String fileDisplayName = null;
    try {           
        IsReport isReport = util.initReport(modelId, planId,request);
        fileDownloadName = isReport.getRptFileName();
        fileDisplayName = isReport.getRptName();

// fileDisplayName = URLEncoder.encode(fileDisplayName, "UTF-8");
fileDisplayName = new String(fileDisplayName.getBytes("UTF-8"),"ISO-8859-1");
} catch (Exception e) {
System.out.println("报告不存在");
e.printStackTrace();
}
OutputStream outp = null;
BufferedInputStream bins = null;
FileInputStream in = null;
BufferedOutputStream bous = null;
try {

in = new FileInputStream(new File(fileDownloadName));
bins = new BufferedInputStream(in);

byte[] b = new byte[bins.available()];
bins.read(b);
bins.close();
response.reset();
response.addHeader("Content-Disposition", "attachment;filename=" + fileDisplayName);
response.addHeader("Content-Length", "" + new File(fileDownloadName).length());
outp = response.getOutputStream();
bous = new BufferedOutputStream(outp);
response.setContentType("application/octet-stream");
bous.write(b);
bous.flush();
bous.close();
} catch (Exception e) {
System.out.println("文件下载失败!");
e.printStackTrace();
} finally {
if (in != null) {
in.close();
in = null;
}
if (outp != null) {
outp.close();
outp = null;
}
if (bins != null) {
bins.close();
bins = null;
}
if (bous != null) {
bous.close();
bous = null;
}
}

    return response;
}
  • 写回答

6条回答

  • zxgmdzz 2017-11-29 08:39
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。