2019独角兽企业重金招聘Python工程师标准>>>
需要
FlexPaperViewer.swf
pdf2swf.exe
jsp部分
art.dialog.open(getRootPath() + '/contract/contract!lookContractPDF.xhtml?conid='+conid,{title: '合同PDF阅读窗', width: 750, height: "100%"});
显示PDF的JSP代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.hwxx.utils.CConst"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
">
java后台
FlexPaperView阅读时是调取.swf文件,所以在上传pdf时最好转换一份成swf文件
上传并转换的代码
/**
* 上传合同PDF文件
* @return
* @throws IOException
*/
public void uploadPDF() throws IOException{
String contractid = reqParam("contractid");
String fileInputFileName = reqParam("fileInputFileName");
ActionContext ctx = ActionContext.getContext();
HttpServletResponse sd = ServletActionContext.getResponse();
sd.setContentType("UTF-8");
String path= request.getSession().getServletContext().getRealPath("images/uploadPDF");
File fd=new File(path);
if(!fd.exists()){
fd.mkdir();
}
try {
DateFormat formater = new SimpleDateFormat("yyyyMMddHHmmss");
String picname = formater.format(new Date());
String savefile = null;
Contract contract = contractService.findbyid(getInt(contractid));
//保存图片信息
if(file!=null){
savefile = picname + fileInputFileName.substring(fileInputFileName.lastIndexOf("."));
FileUtils.copyFile(file, new File(fd ,savefile));
//把pdf文件转为swf文件
Pdf2SwfUtil.pdf2swf(request, savefile);
contract.setPdfname(picname);
}
//根据conid找出对应合同,写入合同信息
contract.setModifier(getEmpNum());
contract.setModifiertime(DateUtil.getDateNow("yyyy-MM-dd"));
recordLog("上传合同PDF文件-[合同编号:"+contract.getContractlabel()+"]");
contractService.update(contract);
JsonObject jo = new JsonObject();
jo.addProperty("success",true);
jo.addProperty("msg","上传成功");
Gson gson = new Gson();
System.out.println(gson.toJson(jo));
response.setContentType("text/html;charset=utf-8");
response.getWriter().write(gson.toJson(jo));
} catch (IOException e) {
JsonObject jo = new JsonObject();
jo.addProperty("success",true);
jo.addProperty("msg","上传成功");
Gson gson = new Gson();
System.out.println(gson.toJson(jo));
response.setContentType("text/html;charset=utf-8");
response.getWriter().write(gson.toJson(jo));
e.printStackTrace();
}
}
Pdf2SwfUtil工具类
public class Pdf2SwfUtil {
/**
* 将pdf文件转换为swf文件
* @param name
* @return
* @throws IOException
*/
public static boolean pdf2swf(HttpServletRequest request, String name)
{
try
{
//获取 pdf文件夹的路径
String pdfPath = getDirectory(request, "images\\uploadPDF");
//获取pdf文件夹中的某个文件路径
String pdfFile = pdfPath.concat("\\" + name);
//获取swf文件夹的路径
String swfPath = getDirectory(request, "images\\swf");
//获取swf文件夹中的某个文件路径
String swfFile = swfPath.concat("\\" + name);
//调用tools文件夹中的pdf2swf工具
String toolsPath = getDirectory(request, "SWFTools");
Runtime run = Runtime.getRuntime();
Process process = run.exec(toolsPath+ "\\pdf2swf.exe -t " + pdfFile
+ " -o " + swfFile.replace(".pdf",".swf")+" -T 9 ".replace("Program Files","'Program Files'"));
process.waitFor();
return true;
} catch (Exception e)
{
return false;
}
}
/**
* 获取某个文件夹的路径
* @param request
* @param directoryName
* @return
*/
public static String getDirectory(HttpServletRequest request, String directoryName)
{
String directory = request.getSession().getServletContext()
.getRealPath(directoryName);
return directory;
}
}