批量上传+类似百度文库在线预览功能
来源:互联网 发布:紧急域名网页升级访问 编辑:程序博客网 时间:2024/06/05 05:39
最近项目遇到这么一个需求,完成至于做以记录。
首先,批量上传用的是Jquery的uploadify,这个可以直接从官网中下到http://www.uploadify.com/
<script type="text/javascript"> var fileName = $(document).ready(function(){ $('#file_upload').uploadify( { 'uploader' : 'uploadify/uploadify.swf', 'script' : 'upload/upload_uploadImage.action', //需要跳转的action 'fileDataName' : 'myfile', //这个很重要这个名称必须要和后面input的name,action中的文件名称相同 'multi' : true,//是否批量'buttonText' : 'UpLoad', //按钮上显示的图片 'cancelImg' : 'uploadify/cancel.png', 'folder' : '', 'sizeLimit':'209715200', 'auto' : false, //自动上传 onComplete: function (event, queueID, fileObj, response, data) {fileName = response;//这是回调函数,当上传完成以后执行 $('p').html(response+"上传成功"); }, onAllComplete: function(event, data) { alert(data.filesUploaded + '个文件上传完毕!'); location.href = "look.jsp?fileName="+fileName; }, onError : function (event, ID, fileObj, errorObj) { if (errorObj.type === "File Size"){ alert('超过文件上传大小限制(2M)!'); return; } alert(errorObj.type + ', Error: ' + errorObj.info); }}); }); </script><input id="file_upload" type="file" name="myfile" /><a href="javascript:jQuery('#file_upload').uploadifyUpload()">开始上传</a>
后台代码
private File myfile; private String myfileFileName; private static final int BUFFER_SIZE = 16*1024; //文件的拷贝 private static boolean copy(File src,File dst){ InputStream in=null; OutputStream out=null; try { in = new BufferedInputStream(new FileInputStream(src),BUFFER_SIZE); out = new BufferedOutputStream(new FileOutputStream(dst),BUFFER_SIZE); byte []buffer=new byte[BUFFER_SIZE]; int len=0; while((len=in.read(buffer))>0){ out.write(buffer, 0, len); } } catch (FileNotFoundException e) { e.printStackTrace(); return false; } catch (IOException e) { e.printStackTrace(); return false; }finally{ try { if(in!=null) in.close(); if(out!=null) out.close(); } catch (IOException e) { e.printStackTrace(); } } return true; }
文件上传完成后就是展示的问题了,目前常用的思路就是先将文件转换为pdf,然后转换为flash,然后展示出来。下面就只实现这一思路的代码
首先,准备工作:
1.下载OpenOffice
2.下载SWFTOOlS
3.下载OpenOffice要用的jar包jodconverter
4.安装上述程序,文件转换就靠它们了,导入jodconverterjar包到项目工程中
5.OpenOffice需要启动器服务,在dos下,在你的安装目录后执行 soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
如下:
准备工作完成后,转换代码如下:
/*** * 将文件转换为pdf * @param input * @param output */public static void convertSTW(String input, String output) {DocumentFormat stw = new DocumentFormat("OpenOffice.org 1.0 Template",DocumentFamily.TEXT, "application/vnd.sun.xml.writer", "stw");DefaultDocumentFormatRegistry formatReg = new DefaultDocumentFormatRegistry();DocumentFormat pdf = formatReg.getFormatByFileExtension("pdf");File inputFile = new File(input);File outputFile = new File(output);OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);try {connection.connect();DocumentConverter converter = new OpenOfficeDocumentConverter(connection);converter.convert(inputFile, stw, outputFile, pdf);} catch (Exception e) {e.printStackTrace();} finally {try {if (connection != null) {connection.disconnect();connection = null;}} catch (Exception e) {}}}/** * 将PDF文档转换为swf格式的FLASH文件. 运行该函数需要用到SWFTools, 下载地址为 * http://www.swftools.org/download.html * * <pre> * 示例: * String sourcePath = "F:\\PDF\\source.pdf"; * String destFile = "F:\\SWF\\dest.swf"; * try { * Converter.pdf2SWF(sourcePath, destFile); * } catch (IOException e) { * e.printStackTrace(); * } * </pre> * * @param sourceFile * 源文件(即PDF文档)路径, 包括源文件的文件名. 示例: D:\\PDF\\source.pdf * @param destFile * 目标文件路径, 即需要保存的文件路径(包括文件名). 示例: D:\\SWF\\dest.swf * @return 操作成功与否的提示信息. 如果返回 -1, 表示找不到源PDF文件, 或配置文件url.properties配置错误; 如果返回 * 0, 则表示操作成功; 返回1或其他, 则表示转换失败 */public static int pdf2SWF(String sourceFile, String destFile)throws IOException {// 目标路径不存在则建立目标路径File dest = new File(destFile);if (!dest.getParentFile().exists())dest.getParentFile().mkdirs();// 源文件不存在则返回 -1File source = new File(sourceFile);if (!source.exists())return -1;String SWFTools_HOME = getPath("pdf2SWFPath");// SWFTools的安装路径。在我的项目中,我为了便于拓展接口,没有直接将SWFTools的安装路径写在这里,详见附件// 如果从文件中读取的URL地址最后一个字符不是 '\',则添加'\'try {// 调用pdf2swf命令进行转换swfextract -i - sourceFilePath.pdf -o// destFilePath.swfString command = SWFTools_HOME + " -t \"" + sourceFile+ "\" -o \"" + destFile + "\" -s flashversion=9 ";Process pro = Runtime.getRuntime().exec(command);BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(pro.getInputStream()));while (bufferedReader.readLine() != null) {}pro.waitFor();return pro.exitValue();} catch (InterruptedException e) {e.printStackTrace();} catch (IOException e1) {e1.printStackTrace();}return 1;}public static String getPath(String key) throws IOException {FileSwitch file = new FileSwitch();String path = file.getClass().getClassLoader().getResource("/").getPath();InputStream in = new BufferedInputStream(new FileInputStream(path+ "/config.properties"));Properties p = new Properties();p.load(in);return p.getProperty(key);}转换工作完成后就是展示的问题了,我这里用的是flexpaper
<a id="viewerPlaceHolder" style="width:680px;height:480px;display:block"></a> <script type="text/javascript"> var fileName = location.search.split('=')[1].split('.')[0]; var fp = new FlexPaperViewer( 'FlexPaperViewer', 'viewerPlaceHolder', { config : { SwfFile : escape('/swfPath/'+fileName+'.swf'), Scale : 0.6, ZoomTransition : 'easeOut', ZoomTime : 0.5, ZoomInterval : 0.2, FitPageOnLoad : true, FitWidthOnLoad : false, PrintEnabled : true, FullScreenAsMaxWindow : false, ProgressiveLoading : false, MinZoomSize : 0.2, MaxZoomSize : 5, SearchMatchAll : false, InitViewMode : 'Portrait', ViewModeToolsVisible : true, ZoomToolsVisible : true, NavToolsVisible : true, CursorToolsVisible : true, SearchToolsVisible : true, localeChain: 'en_US' }}); </script>
最后对开发中遇到的问题坐总结吧:
1.在pdf转flash的时候可能遇到一些问题,尤其是一些大文件的时候,需要加一些参数,具体的这里就不写了,因为实在太多了,具体的可以在网上搜索“pdf2swf 参数”会有很多
2..因为在转换一些大文件的时间比较长,这样就遇到的线程问题,我目前是使用阻塞队列,一个一个的进行转换
3.linux上部署的时候有些问题
4.flexpaper上有打印和loge,可以在网上找修改的方法和已经修改的flash
本来想放一个做的demo的,可是一直传不上去,有需求的朋友可以加我qq:358333128
- 批量上传+类似百度文库在线预览功能
- 类似百度文库在线预览文档flash swf文库预览
- 类似百度文库的在线浏览功能
- 类似百度文库的文档在线预览实现方法
- 实现类似百度文库的在线预览office文档
- 类似百度文库在线预览文件内容技术探讨
- PHP 仿百度文库,实现Word文档在线预览功能
- 实现类似百度文库的文档在线阅读功能
- 在线预览 模仿百度文库
- 类似百度文库在线浏览
- 类似百度文库在线预览文档flash版(支持word、excel、ppt、pdf)+在线预览文档html版
- 类似百度文库在线预览文档flash版(支持word、excel、ppt、pdf)+在线预览文档html版
- 类似百度文库在线预览文档flash版(支持word、excel、ppt、pdf)+在线预览文档html版
- 类似百度文库在线预览文档flash版(支持word、excel、ppt、pdf)+在线预览文档html版
- 类似百度文库在线预览文档flash版(支持word、excel、ppt、pdf)+在线预览文档html版
- 类似百度文库在线预览文档flash版(支持word、excel、ppt、pdf)
- 仿百度文库实现文档在线预览
- 利用flexpaper开发类似百度文库功能,在线浏览WORD文档
- silverlight IsolatedStorageFile 创建及操作xml
- JAVA EE 项目常用知识 之AJAX技术实现select下拉列表联动的两种用法(让你真正理解ajax)
- 【求助】求D3D8多次初始化的一个问题
- SCSI接口协议,SAS,磁盘驱动器,磁盘控制器
- 业务规则引擎—规则池功能学习
- 批量上传+类似百度文库在线预览功能
- Boost - 序列化 (Serialization)
- java的HashMap与ConcurrentHashMap
- 安卓自动化测试工具MonkeyRunner之使用ID
- Invalid layout param in a LinearLayout: layout_toLeftOf
- poj 2339 Rock, Scissors, Paper
- 论文笔记
- 解决Mac机下javac乱码问题
- 一个有趣的抛硬币问题