用户登录
用户注册

分享至

基于FlashPaper实现JSP在线阅读代码示例

  • 作者: 小耀光
  • 来源: 51数据库
  • 2021-07-12

flashpaper 是macromedia推出的一款电子文档类工具,通过使用本程序,你可以将需要的文档通过简单的设置转换为swf格式的flash动画,原文档的排版样式和字体显示不会受到影响,这样做的好处是不论对方的平台和语言版本是什么,都可以自由的观看你所制作的电子文档动画,并可以进行自由的放大,缩小和打印,翻页等操作,对文档的传播非常有好处,而且由于flash动画的普及性和强大的可调节性。

这里使用struts2进行文件上传,后台处理转换,并将文件存放到工程目录

前台使用swfobject_2_2显示flash文件

首先请安装flashpaper,其次把swfobject_2_2.zip解压然后把expressinstall.swf和swfobject.js拷贝到工程的根目录(仅仅是为了测试)

编写文件上传页面:

<%@ page language="java" contenttype="text/html; charset=utf-8"
	pageencoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
	<head>
		<title>上传文件 关注新浪微博:@java小强</title>
	</head>
	<body>
		<s:form action="upload" method="post" enctype="multipart/form-data">
			<s:file name="file" label="file name"></s:file>
			<s:submit label="ok" onclick="" />
		</s:form>
	</body>
</html>

注意这里使用了struts2的标签

action需要处理上传,然后判断上传文件的类型,调用转换获得转换后文件的路径,并把路径传递到前台:

package com.xidian.cui.action;
import java.io.file;
import javax.servlet.http.httpservletrequest;
import org.apache.struts2.servletactioncontext;
import com.opensymphony.xwork2.actionsupport;
import com.xidian.cui.util.flashpaper;
/**
 * @说明 上传执行
 * @author cuisuqiang
 * @version 1.0
 * @since 关注新浪微博:@java小强
 */
@suppresswarnings("serial")
public class fileuploadaction extends actionsupport {
	private file file;
	private string filefilename;
	private string filecontenttype;
	
	@suppresswarnings("deprecation")
	public string execute() {
		httpservletrequest request = servletactioncontext.getrequest();
		string tempname = flashpaper.converter(file, filecontenttype,filefilename, request);
		// 把转换的路径放到请求中
		request.setattribute("tempname", tempname);
		return "suc";
	}
	/**
	 * 获得不同文件的后缀
	 */
	public void setfilecontenttype(string filecontenttype) {
		if (filecontenttype.contains("msword")) {
			this.filecontenttype = ".doc";
		} else if (filecontenttype.contains("excel")) {
			this.filecontenttype = ".xls";
		} else if (filecontenttype.contains("pdf")) {
			this.filecontenttype = ".pdf";
		} else if (filecontenttype.contains("powerpoint")) {
			this.filecontenttype = ".ppt";
		} else {
			this.filecontenttype = ".doc";
		}
	}
	public file getfile() {
		return file;
	}
	public void setfile(file file) {
		this.file = file;
	}
	public string getfilecontenttype() {
		return filecontenttype;
	}
	public string getfilefilename() {
		return filefilename;
	}
	public void setfilefilename(string filefilename) {
		this.filefilename = filefilename;
	}
}

然后是转换类,因为我做的时候发现如果路径过于复杂很容易出错,所以我做的时候先把上传的文件拷贝到一个临时文件夹,然后转换,转换后的swf文件也放到该文件夹,最后拷贝到自己指定的位置。这里指定的位置就是工程目录下,为的是方便显示。

然后我做了一个判断,如果已经转换了该文件,就直接显示。注意这样是不合理的,因为用户可能会上传文件名称一样的文件。但是我这里只是作一个引导,你可以根据自己的业务去记录转换的文件,这样同一个文件转换后不必每次都重复转换,这样做是非常耗时也耗费服务器性能的。

指定文件夹内存放了我们的文件,但是他会非常占用磁盘空间,这里为了测试我没有删除转换后的临时文件,但是在实际使用中,我们应该在后台定义一个定时器,来删除长期没有使用的文件,并且在每次转换前判断这个临时文件夹如果过大的话,就直接清空临时文件夹。

来看转换的代码:

package com.xidian.cui.util;
import java.io.bufferedinputstream;
import java.io.bufferedoutputstream;
import java.io.file;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.inputstream;
import java.io.outputstream;
import java.text.simpledateformat;
import java.util.date;
import javax.servlet.http.httpservletrequest;
/**
 * @说明 将文件转为swf
 * @author cuisuqiang
 * @version 1.0
 * @since 关注新浪微博:@java小强
 */
public class flashpaper extends thread {

	@suppresswarnings("deprecation")
	public static string converter(file srcfile,string filecontenttype,string filefilename, httpservletrequest request) {
		string uploadpath = request.getrealpath("/");
		simpledateformat format = new simpledateformat("yyyy-mm-dd");
		string datafile = format.format(new date());
		uploadpath += "swftemp\" + datafile;
		file f = new file(uploadpath);
		if (!f.exists() && !f.isdirectory()) {
			f.mkdirs();
		}
		// swf 放到服务器下
		string filename = uploadpath + "\" + filefilename + ".swf";
		file file = new file(filename);
		if(file.exists()){
			return "swftemp/" + datafile + "/" + filefilename + ".swf";
		}
		// 要转换的文件 放到临时目录
		string doctempfilename = "c:\" + string.valueof(new date().gettime()) + filecontenttype;
		file doctempfile = new file(doctempfilename);
		copyfile(srcfile, doctempfile);
		// 转换后的文件 放到临时目录
		string swftempfilename = "c:\" + string.valueof(new date().gettime()) + ".swf";
		file swftempfile = new file(swftempfilename);
		try {
			string converter = "c:\\program files\\macromedia\\flashpaper 2\\flashprinter.exe -o " + swftempfile.getabsolutepath() + " " + doctempfile.getabsolutepath();
			runtime pro = runtime.getruntime();
			pro.exec(converter);
			// 注意,为了测试,这里只要没有转换工程,就一直等待
			while(true){
				if(!swftempfile.exists()){
					thread.sleep(1000);
					continue;
				}
				copyfile(swftempfile, file);
				// 删除临时文件
				swftempfile.delete();
				doctempfile.delete();
				return "swftemp/" + datafile + "/" + filefilename + ".swf";
			}
		} catch (exception e) {
			system.out.println("执行失败");
			e.printstacktrace();
		}
		return "";
	}
	private static final int buffer_size = 102400;
	private static void copyfile(file src, file dir) {
		try {
			inputstream input = null;
			outputstream output = null;
			try {
				input = new bufferedinputstream(new fileinputstream(src),
						buffer_size);
				output = new bufferedoutputstream(new fileoutputstream(dir),
						buffer_size);
				byte[] buffer = new byte[buffer_size];
				while (input.read(buffer) > 0) {
					output.write(buffer);
				}
			} catch (exception e) {
				e.printstacktrace();
			} finally {
				if (null != input) {
					input.close();
				}
				if (null != output) {
					output.close();
				}
			}
		} catch (exception e) {
			e.printstacktrace();
		}
	}
}

跳转到页面显示swf文件即可:

<%@ page language="java" import="java.util.*" pageencoding="utf-8"%>
<%
string path = request.getcontextpath();
string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/";
%>
<html>
 <head>
  <base  rel="external nofollow" >
  <title>在线阅读 关注新浪微博:@java小强</title>
  <script type="text/javascript" src="swfobject.js"></script>
  <script type="text/javascript">
		swfobject.registerobject("myid", "9.0.0", "expressinstall.swf");
	</script>
 </head>
 <body>
  		<div>	
			<object id="myid" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="100%" height="100%">
				<param name="movie" value="${tempname }" />
    		<!--[if !ie]>-->
				<object type="application/x-shockwave-flash" data="${tempname }" width="300" height="120">
				<!--<![endif]-->
				<div>
					<h1>alternative content</h1>
					<p><a  rel="external nofollow" ><img src="http://www.51sjk.com/Upload/Articles/1/0/253/253179_20210627001849310.gif" alt="get adobe flash player" /></a></p>
				</div>
				<!--[if !ie]>-->
				</object>
				<!--<![endif]-->
			</object>
		</div>
 </body>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

软件
前端设计
程序设计
Java相关