java输出本地文件到页面显示(图片、pdf文件)

public void findPdf() throws IOException{
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("application/pdf");
// response.setContentType("image/*");  此处不需要图片
// 只要把 response.setContentType("image/*"); 改为 response.setHeader("Content-type", "image/png");
FileInputStream in = new FileInputStream(new File(“c:/test.pdf”));
OutputStream out = response.getOutputStream();
byte[] b = new byte[2048];
// 字节数根据自己实际计算机而定
while ((in.read(b))!=-1) {
out.write(b);
}
out.flush();
in.close();
out.close();
}

 

你可能感兴趣的:(springMVC)