publicString showDocAttach() {
HttpServletRequest request=ServletActionContext.getRequest();
HttpServletResponse response=ServletActionContext.getResponse();
OutputStream outputStream= null;
BufferedInputStream bufferedInputStream= null;try{
extprjdocattachment= "数据库查询java对象";if (extprjdocattachment != null) {//获取存储的文件路径
String path =extprjdocattachment.getFilePath();//获取存储的文件类型
String fileType =extprjdocattachment.getFileType();
File file= newFile(path);if (!file.exists()) {return null;
}
response.setContentType("text/html; charset=UTF-8");//pdf jsp png 直接输出到页面
if ("pdf".equalsIgnoreCase(fileType)) {
response.setContentType("application/pdf");
}else if ("docx".equalsIgnoreCase(fileType)) {//docx 使用aspose转化为pdf文件
String tempPath = "tempPdf" + "/";
Doc2PdfUtil.doc2Pdf(path, tempPath, extprjdocattachment.getFileName());
path= tempPath + extprjdocattachment.getFileName() + ".pdf";
response.setContentType("application/pdf;charset=UTF-8");
}else if ("jpg".equalsIgnoreCase(fileType) || "png".equalsIgnoreCase(fileType)) {
response.setContentType("image/" +fileType);
}
bufferedInputStream= new BufferedInputStream(newFileInputStream(path));
outputStream=response.getOutputStream();int count = 0;byte[] buffer = new byte[1024 * 1024];while ((count = bufferedInputStream.read(buffer)) != -1) {
outputStream.write(buffer,0, count);
}
outputStream.flush();
}
}catch(Exception e) {
e.printStackTrace();
}finally{if (outputStream != null) {try{
outputStream.close();
}catch(IOException e) {
e.printStackTrace();
}
}if (bufferedInputStream != null) {try{
bufferedInputStream.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}return null;
}