参考博文 http://blog.csdn.net/z69183787/article/details/17468039
主要原理
1.通过第三方工具openoffice,将word、excel、ppt、txt等文件转换为pdf文件
2.通过swfTools将pdf文件转换成swf格式的文件
3.通过FlexPaper文档组件在页面上进行展示
准备工作
具体步骤参见原博主文章如上,强调几点
openoffice SWFTools根据自己电脑系统下载最新版本即可
FlexPaper新版本的 js文件夹下不再是flexpaper_flash_debug.js,flexpaper_flash.js,jquery.js
而变成了flexpaper.js flexpaper_handlers.js flexpaper_handlers_debug.js jquery.min.js
这几个文件,拷贝到项目下的js 目录下即可,值得注意的一点是,检查一下导入js的路径,建议在jsp中添加
几行内容
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<head>
这样导入js的时候路径就是从项目根路径下开始算起,比如我的js文件夹建立在项目根路径下导入时就可以写成
<script type="text/javascript" src="Js/jquery.min.js"></script>
还有一点 flexpaper.js 中将 FlexPaperViewer.swf 的路径设定在了项目根路径下,如果按照操作最后打开的网页是一片空白的话
那就是FlexPaperViewer.swf 放置的路径不对,总之,如果结果不如人意,一般就是路径的问题。
fileUpload.jsp documentView.jsp 我基本没做改变,而将docUploadConvertAction.jsp的处理代码放到了后台
(以下为部分代码,要自己按照实际修改,仅提供思路)
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addCommit(@Valid Bug bug, BindingResult bindingResult, HttpServletRequest request,
@RequestParam("files") CommonsMultipartFile[] files, Model model) throws IOException {
。。。
//上传文件的原名(即上传前的文件名字)
String originalFilename = null;
for (int i = 0; i < files.length; i++) {
if(files[i].getOriginalFilename()!=null && files[i].getOriginalFilename()!=""){
// 取文件格式后缀名
originalFilename = files[i].getOriginalFilename();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
String currenttime = dateFormat.format( new Date() );
//String type = originalFilename.substring(originalFilename.indexOf("."));
String filename = currenttime + originalFilename;// 取当前时间戳+文件初始名字作为最终文件名
String path = request.getSession().getServletContext().getRealPath("/upload/" + filename);// 存放位置
File destFile = new File(path);
try {
FileUtils.copyInputStreamToFile(files[i].getInputStream(),destFile);// 复制临时文件到指定目录下
bug.setAttach(filename);
} catch (IOException e) {
model.addAttribute("ferror", "文件[" + originalFilename + "]上传失败");
return "bug/add";
}
}
}
springmvc上传多个文件可以使用@RequestParam("files") CommonsMultipartFile[] files 进行绑定,“files”
获取的是jsp中type="file" name=“files” 中的文件,可以有多个。
/**
* 下载附件
*
* @param attach
* @param request
* @param response
* @return
*/
@RequestMapping(value = "/{attach}/download", method = RequestMethod.GET)
public String download(@PathVariable("attach") String attach, HttpServletRequest request,
HttpServletResponse response) {
response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition", "attachment;fileName="
+ attach);
try {
String path = request.getSession().getServletContext().getRealPath("/upload/" + attach);// 存放位置
InputStream inputStream = new FileInputStream(new File(path));
OutputStream os = response.getOutputStream();
byte[] b = new byte[2048];
int length;
while ((length = inputStream.read(b)) > 0) {
os.write(b, 0, length);
}
// 这里主要关闭。
os.close();
inputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 返回值要注意,要不然就出现下面这句错误!
//java+getOutputStream() has already been called for this response
return null;
}
//预览方法
@RequestMapping("/{attach}/view")
public String view(@PathVariable("attach") String attach,HttpServletRequest request,Model model){
String path = request.getSession().getServletContext().getRealPath("/upload/" + attach);// 存放位置
DocConverter d = new DocConverter(path);
//调用conver方法开始转换,先执行doc2pdf()将office文件转换为pdf;再执行pdf2swf()将pdf转换为swf;
d.conver();
//调用getswfPath()方法,打印转换后的swf文件路径
System.out.println(d.getswfPath());
//生成swf相对路径,以便传递给flexpaper播放器
String swfpath = "upload"+d.getswfPath().substring(d.getswfPath().lastIndexOf("/"));
System.out.println(swfpath);
//将相对路径放入sessio中保存
model.addAttribute("swfpath", swfpath);
return "file/documentView";
}
将新生成的swf的地址传给documentView.jsp 就可以了