java后台导出pdf

新页面打开wpf

@RequestMapping("/showPdf")

public String getpic( HttpServletRequest request, HttpServletResponse response) throws IOException {

File file = new File("C://Users//Administrator//Desktop//hello.pdf");

if (!file.exists()) {

request.setAttribute("error", "附件已删除或不存在");

// return "/error";

}

InputStream in = null;

OutputStream os = null;

try {

response.setContentType("application/pdf"); // 设置返回内容格式

in = new FileInputStream(file); //用该文件创建一个输入流

os = response.getOutputStream(); //创建输出流

byte[] b = new byte[1024];

while (in.read(b) != -1) {

os.write(b);

}

in.close();

os.flush();

os.close();

} catch (Exception e) {

try {

if (null != in) {

in.close();

}

} catch (IOException e1) {

e1.printStackTrace();

}

try {

if (null != os) {

os.close();

}

} catch (IOException e2) {

e2.printStackTrace();

}

}

return null;

}