09.JAVA代码实现PDF转换
@RequestMapping(value="/pdfDownLoad")
public void pdfDownLoad(String reportName, HttpServletRequest request, HttpServletResponse response)throws RollbackableBizException{
try {
reportName=new String(reportName.getBytes("iso8859-1"),"UTF-8");
String path = Thread.currentThread().getContextClassLoader().getResource("/").getPath();
path = path.replace("/WEB-INF/classes/", "");
String pdfFilePath = path+"/pages/workflow/instance/";
/**
* 路径等待修改
*/
File file = new File(pdfFilePath+reportName);
try{
String fileName=null;
//火狐浏览器
if (request.getHeader("USER-AGENT").toLowerCase().indexOf("firefox") != -1) {
fileName= new String(reportName.getBytes("UTF-8"), "ISO-8859-1");
}else{
fileName = URLEncoder.encode(reportName, "UTF-8");//encode编码UTF-8 解决大多数中文乱码
fileName = fileName.replace("+", "%20");//encode后替换空格 解决空格问题
}
response.setHeader("content-disposition", "attachment;filename="+fileName);
response.setContentType(getServletContext().getMimeType(fileName));
response.setCharacterEncoding("UTF-8");
BufferedInputStream bis =new BufferedInputStream (new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
int b;
while((b=bis.read())!=-1){
bos.write(b);
}
bis.close();
bos.close();
}catch(Exception e){
e.printStackTrace();
}finally{
//file.delete();
}
} catch (Exception e) {
e.printStackTrace();
logger.error("根据用户ID获取流程实例信息失败!!"+e.getMessage());
}
}
public void pdfDownLoad(String reportName, HttpServletRequest request, HttpServletResponse response)throws RollbackableBizException{
try {
reportName=new String(reportName.getBytes("iso8859-1"),"UTF-8");
String path = Thread.currentThread().getContextClassLoader().getResource("/").getPath();
path = path.replace("/WEB-INF/classes/", "");
String pdfFilePath = path+"/pages/workflow/instance/";
/**
* 路径等待修改
*/
File file = new File(pdfFilePath+reportName);
try{
String fileName=null;
//火狐浏览器
if (request.getHeader("USER-AGENT").toLowerCase().indexOf("firefox") != -1) {
fileName= new String(reportName.getBytes("UTF-8"), "ISO-8859-1");
}else{
fileName = URLEncoder.encode(reportName, "UTF-8");//encode编码UTF-8 解决大多数中文乱码
fileName = fileName.replace("+", "%20");//encode后替换空格 解决空格问题
}
response.setHeader("content-disposition", "attachment;filename="+fileName);
response.setContentType(getServletContext().getMimeType(fileName));
response.setCharacterEncoding("UTF-8");
BufferedInputStream bis =new BufferedInputStream (new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
int b;
while((b=bis.read())!=-1){
bos.write(b);
}
bis.close();
bos.close();
}catch(Exception e){
e.printStackTrace();
}finally{
//file.delete();
}
} catch (Exception e) {
e.printStackTrace();
logger.error("根据用户ID获取流程实例信息失败!!"+e.getMessage());
}
}