word在线问题

1.js代码如下

var sdata = "";
$(function(){
var pathdoc = path.split(".");
var explorer =navigator.userAgent ;
if(pathdoc[1]=="pdf"){
$("#printid").attr("src","../file_FileDownload_prints.action?path="+path);
}
if(pathdoc[1]=="doc"){
if((window.ActiveXObject || "ActiveXObject" in window)){
$("#printid").hide();
$("#1c").show();
var r = $("#1c");
var y = {};
//把自定义控件word 的各个属性存到数组中
y['id'] = r.attr("id");
y['ctype'] = 'word';
y['height'] = r.attr("height");
y['width'] = r.attr("weight");
y['uname'] = '';
y['ucode'] = '';
y['ctime'] = webform.getTime();
var cJ= webform;
if (cJ) {
y['uname'] = cJ.getUserName();
y['ucode'] = cJ.getUser();
cJ = null
}
//定义一个新的office方法,将属性数组y闯过去
var cT = new Office(y);
g_ctrls[r.attr("id")] = cT;
cT.render(r);
}else{
webform.confirm("是否下载word打印?",downloadword,"","","",wordclose);
}
}

})

function downloadword(){
$("#printid").attr("src","../file_FileDownload_prints.action?path="+path);
$(parent.document).find(".co-win-gray").hide();
$(parent.document).find(".co-win-uploaddiv").hide();
}

function wordclose(){
$(parent.document).find(".co-win-gray").hide();
$(parent.document).find(".co-win-uploaddiv").hide();
}

Office = function(c) {
this.divid = c['id'];
this.myobj = null;
this.ctype = c['ctype'];
this.shost = window.location.href.substr(0, window.location.href.lastIndexOf('cops'));
//word控件打开的时候执行的事件
this.openFile = function() {
try{
var a = '';
a = this.shost+"/file_FileDownload_fileUploadword.action?path="+path;
try {
//文件名为空,打开后台原始给出的文件
if (this.ctype == 'word') {
this.myobj.Open(a, true, "Word.Document");
}
} catch (e) {
a = '';
}
if (a == '') {
return;
}
//this.myobj封装好的方法
/*this.myobj.ProtectDoc(0, 2, "0x0xx0x0x0x");*/
/*this.myobj.ShowRevisions(1);
this.myobj.SetTrackRevisions(1);
this.myobj.SetCurrUserName(c['uname']);
this.myobj.SetCurrTime(c['ctime']);
this.myobj.Titlebar = false;*/
/*this.myobj.Menubar = false;*/
a = '';
}catch(e){
return;
}
}
//表单加载后执行的事件,执行打开word
this.render = function(a) {
a.append('<object id="' + c['id'] + 'x' + '" name="' + c['id'] + 'x' + '" style="visibility:visible;left: 0px; width: 100%; top: 0px; height: 100%;" classid="clsid:00460182-9E5E-11D5-B7C8-B8269041DD57" codebase="dsoframer.ocx#version=2,3,0,1"><PARAM NAME="_ExtentX" VALUE="19923"><PARAM NAME="_ExtentY" VALUE="16325"><PARAM NAME="BorderColor" VALUE="-2147483632"><PARAM NAME="BackColor" VALUE="-2147483643"><PARAM NAME="ForeColor" VALUE="-2147483640"><PARAM NAME="TitlebarColor" VALUE="-2147483635"><PARAM NAME="TitlebarTextColor" VALUE="-2147483634"><PARAM NAME="BorderStyle" VALUE="1"><PARAM NAME="Titlebar" VALUE="0"><PARAM NAME="Toolbars" VALUE="1"><PARAM NAME="Menubar" VALUE="1"><param name="_ExtentX" value="21458"><param name="_ExtentY" value="6350"><param name="BorderColor" value="-2147483632"><param name="BackColor" value="-2147483643"><param name="ForeColor" value="-2147483640"><param name="TitlebarColor" value="-2147483635"><param name="TitlebarTextColor" value="-2147483634"><param name="BorderStyle" value="1"><param name="Titlebar" value="0"><param name="Toolbars" value="1"><param name="Menubar" value="1"></object>');
try{
this.myobj = document.all[c['id'] + 'x'];
this.openFile();
window.scrollTo(c['height'])
}catch(e){
return;
}
}
};

 

2.java后台代码如下

public void fileUploadword() throws IOException{
if(getWriter() == false) return;
String path = request.getParameter("path");
path = Resource.getValue("temp")+ "Temp\\"+path;
//out.write(relativelyPath);
OpenFileByStream(path,true);
}

 

public void OpenFileByStream(String strUrl,Boolean isLocal) {

if(isLocal != true){
strUrl = request.getRealPath(strUrl.substring(strUrl.indexOf('/') + 1)).replace("\\", "/");
try {
strUrl = java.net.URLDecoder.decode(strUrl, "UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
}


File fi = new File(strUrl);
boolean fileIs = (fi.exists());
if (fileIs) { //如果文件存在
response.reset();
response.setCharacterEncoding("gb2312");
// response.addHeader("WWW-Authenticate", "BASIC");
response.setHeader("WWW-Authenticate", "BASIC");
// response.setHeader("Cache-Control", "no-cache");
// response.setHeader("Pragma", "no-cache");

String extName = strUrl.substring(strUrl.lastIndexOf("."),
strUrl.length()).toUpperCase(); //后缀名转换为大写
boolean strName = (extName.endsWith(".DOC")
|| extName.endsWith(".DOCX") || extName.endsWith(".PDF")
|| extName.endsWith(".PPT") || extName.endsWith(".PPTX")
|| extName.endsWith(".TXT") || extName.endsWith(".XLS")
|| extName.endsWith(".XLSX") || extName.endsWith(".BMP")
|| extName.endsWith(".GIF") || extName.endsWith(".IEF")
|| extName.endsWith(".JPEG") || extName.endsWith(".JPG")
|| extName.endsWith(".PNG") || extName.endsWith(".TIFF")
|| extName.endsWith(".TIF"));

if (strName) {
if (".DOC".equals(extName)) {
response.setHeader("Content-type","application/msword");
} else if (".DOCX".equals(extName)) {
response.setHeader("Content-type","application/vnd.openxmlformats-officedocument.wordprocessingml.document");
} else if (".PDF".equals(extName)) {
response.setHeader("Content-type","application/pdf");
} else if (".TXT".equals(extName)) {
response.setHeader("Content-type","text/html");
} else if (".XLS".equals(extName)) {
response.setHeader("Content-type","application/vnd.ms-excel");
} else if (".XLSX".equals(extName)) {
response.setHeader("Content-type","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
} else if (".PPT".equals(extName)) {
response.setHeader("Content-type","application/vnd.ms-powerpoint");
} else if (".PPTX".equals(extName)) {
response.setHeader("Content-type","application/vnd.openxmlformats-officedocument.presentationml.presentation");
} else if (".BMP".equals(extName)) {
response.setHeader("Content-type","image/bmp");
} else if (".GIF".equals(extName)) {
response.setHeader("Content-type","image/gif");
} else if (".IEF".equals(extName)) {
response.setHeader("Content-type","image/ief");
} else if (".JPEG".equals(extName)) {
response.setHeader("Content-type","image/jpeg");
} else if (".JPG".equals(extName)) {
response.setHeader("Content-type","image/jpeg");
} else if (".PNG".equals(extName)) {
response.setHeader("Content-type","image/png");
} else if (".TIFF".equals(extName)) {
response.setHeader("Content-type","image/tiff");
} else if (".TIF".equals(extName)) {
response.setHeader("Content-type","image/tif");
}

/*
* try { TransmitFile.downLoad(response, strUrl);
* response.flushBuffer(); } catch (Exception e) {
* e.printStackTrace(); }
*/

BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
response.reset();
response.setCharacterEncoding("gb2312");
response.setContentType("application/x-msdownload");

String filename = strUrl.substring(
strUrl.lastIndexOf("/") + 1, strUrl.length());

// response.setHeader("Content-Disposition","attachment;filename="+new
// String(filename.getBytes("gb2312"),"iso8859-1"));
response.setHeader("Content-Disposition", "filename="
+ new String(filename.getBytes("gb2312"),
"iso8859-1"));
bis = new BufferedInputStream(new FileInputStream(strUrl));

bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesread;
while (-1 != (bytesread = bis.read(buff, 0, buff.length))) { //读取文件
bos.write(buff, 0, bytesread);
}
response.flushBuffer(); //将缓冲区的内容输出出来
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close(); //关闭流
} catch (IOException e) {
e.printStackTrace();
}
}
if (bos != null) {
try {
bos.close(); //关闭流
} catch (IOException e) {
e.printStackTrace();
}
}
}
// out.flush();
// out.close();
} else {
out.write("The file type is not supported in view.");
}
}
else{
out.write("The file is not exist.");
}
}、

 

 

posted @ 2017-12-12 13:59  Blue牛仔裤  阅读(249)  评论(2编辑  收藏  举报