您的位置:首页 > 编程语言 > Java开发

使用北京点聚信息技术有限公司的WebOffice工具上传文件

2012-12-27 14:55 411 查看
在北京点聚信息技术有限公司的SDK文档中关于文件上传有下面这一段话:

因为Web页面上的WebOffice打开的文件无法随表单记录提交到处理页面,所以我们把文件模拟成表单的文件域<input type=file …>,通过接口把文件和表单元素发送到保存记录的页面(过程类似表单的提交)。

上面这段意思无非就是不能用提交表单的方式上传文件,因为weboffice打开的文件没有在表单里面。但是它自己做了一个接口用来解决这个问题。

         //
添加上传文件
         document.all.WebOffice1.HttpAddPostCurrFile("AipFile ","");  
       用来对当前页面在weboffice里面打开的文件进行上传提交,效果与提交表单是一样的。

/****************************************************
*                                            上传文档
*****************************************************/
function SaveDoc(id,docType) {
         try{
                  
var webObj=document.getElementById("WebOffice1");
                  
var returnValue;
                  
webObj.HttpInit();                          //初始化Http引擎
                  
// 添加相应的Post元素
                  
webObj.HttpAddPostString("id", id);
                  
webObj.HttpAddPostString("DocType",docType);
                  
webObj.HttpAddPostCurrFile("DocContent","");               //
上传文件
                  
returnValue = webObj.HttpPost("/TestUploadAction");      //
判断上传是否成功
                  
if("succeed" == returnValue){
                           alert("文件上传成功");        
                  
}else if("failed" == returnValue)
                           alert("文件上传失败");
                  
//return_onclick();
         }catch(e){
                  
alert("异常\r\nError:"+e+"\r\nError Code:"+e.number+"\r\nError Des:"+e.description);
         }
}
         这段代码的效果跟下面这段代码的效果是一样的,只不过下面代码中file只能是指定路径上传,而在上面的代码中是在页面中嵌入的doc可以直接上传到服务器,不管是否已经保存到本地。

       <form action="TestUploadAction" name="myform" id="myform">
                  
<input type="text" name="id"/>
                  
<input type="text" name="DocType">
                  
<input type="file" name="DocContent">
         </form>
         <script type="text/javascript">
                  
myform.submit();
         </script>
         Struts里面配置Action

       <action name="TestUploadAction" class="TestUploadAction" method="Function">
         </action>
       Spring配置Bean

<bean id="TestUploadAction" class="test.TestUploadAction">
                  
<property name="fs" ref="FileService"></property>
         </bean>
         <!--
文件管理service的Bean -->
         <bean id="FileService" class="net.ib.util.service.impl.FileServiceImpl">
         </bean>
       TestUploadAction.java

 /**
 * -------------------------------------------------------------------------------------
 * |
文件名:    TestUploadAction.java
 * |
包名:test
 * |
描述:TODO(用一句话描述该文件做什么)
 * |
作者:xiaokai
 * |
创建日期:2012-12-18 下午4:29:58
 * |
版本号: V1.0
 * |------------------------------------------------------------------------------------
 * |
修订记录:
 * |    1、2012-12-18下午4:29:58
 * |------------------------------------------------------------------------------------
 */
package test;
 
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.http.HttpServletResponse;
 
import org.apache.struts2.ServletActionContext;
 
import net.ib.util.service.FileService;
 
 
  /**
 * <p>类名:test.TestUploadAction </p>
 * <p>描述:TODO(用一句话描述该文件做什么)</p>
 * <p></p>
 */
public class TestUploadAction {
         private        String         id;
         private        String         DocTitle;
         private        String         DocID;
         private        String         DocType;
         private        File   DocContent;        //上传文件
    private String DocContentFileName; //上传文件名(注意这里命名一定是上传文件+FileName)
    private String DocContentContentType; //上传文件MIME类型(命名:上传文件+ContentType)
    private         FileService fs;
         public FileService getFs() {
                  
return fs;
         }
         public void setFs(FileService fs) {
                  
this.fs = fs;
         }
         public String getId() {
                  
return id;
         }
         public void setId(String id) {
                  
this.id = id;
         }
         public String getDocTitle() {
                  
return DocTitle;
         }
         public void setDocTitle(String docTitle) {
                  
DocTitle = docTitle;
         }
         public String getDocID() {
                  
return DocID;
         }
         public void setDocID(String docID) {
                  
DocID = docID;
         }
         public String getDocType() {
                  
return DocType;
         }
         public void setDocType(String docType) {
                  
DocType = docType;
         }
         public File getDocContent() {
                  
return DocContent;
         }
         public void setDocContent(File docContent) {
                  
DocContent = docContent;
         }
         public String getDocContentFileName() {
                  
return DocContentFileName;
         }
         public void setDocContentFileName(String docContentFileName) {
                  
DocContentFileName = docContentFileName;
         }
         public String getDocContentContentType() {
                  
return DocContentContentType;
         }
         public void setDocContentContentType(String docContentContentType) {
                  
DocContentContentType = docContentContentType;
         }
        
         public         String         Function(){
                  
Boolean      flag   =       fs.FileUpload("c:\\","test", DocContent, DocContentFileName, DocContentContentType);
                  
HttpServletResponse response = ServletActionContext.getResponse();
                  
response.setCharacterEncoding("utf-8");
                  
if(flag){
                           try {
                                    PrintWriter pw = response.getWriter();
                                    pw.print("succeed");
                                    pw.close();
                           } catch (IOException e) {
                                    e.printStackTrace();
                           }
                  
}
                  
else{
                           try {
                                    PrintWriter pw = response.getWriter();
                                    pw.print("failed");
                                    pw.close();
                           } catch (IOException e) {
                                    e.printStackTrace();
                           }
                  
}
                  
return null;
         }
}
 
         FileServiceImpl.java

 /**
 * -------------------------------------------------------------------------------------
 * |
文件名:    FileServiceImpl.java
 * |
包名:net.ib.util.service.impl
 * |
描述:TODO(用一句话描述该文件做什么)
 * |
作者:xiaokai
 * |
创建日期:2012-12-18 下午4:00:20
 * |
版本号: V1.0
 * |------------------------------------------------------------------------------------
 * |
修订记录:
 * |    1、2012-12-18下午4:00:20
 * |------------------------------------------------------------------------------------
 */
package net.ib.util.service.impl;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
 
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
 
import net.ib.util.service.FileService;
 
public class FileServiceImpl implements FileService {
         private static Logger logger = Logger
                           .getLogger(FileServiceImpl.class);
          /* (非-Javadoc)
          * <p>名称: FileUpload</p>
          * <p>说明:指定名称与路径上传文件</p>
          * @param savePath                      上传文件保存路径
          * @param saveAsName               上传文件另存为名称
          * @param file                               上传文件
          * @param FileName                    上传文件名称
          * @param FileContentType上传文件类型
          * @return                                      true:上传成功,false:上传失败
          * @see net.ib.util.service.FileService#FileUpload(java.lang.String, java.lang.String, java.io.File, java.lang.String, java.lang.String)
          */
         @Override
         public Boolean FileUpload(String savePath, String saveAsName, File file,
                           String FileName, String FileContentType) {
                  
// TODO Auto-generated method stub
                  
String         path=savePath;
                  
File Dir = new File(path);
                  
// 如果路径不存在就创建它
                  
if (!Dir.exists()) {
                           Dir.mkdir();
                  
}
                  
// 获取文件上传时间,作为文件名的一部分
                  
Date Mydate = new Date();
                  
long NowTime = Mydate.getTime();
                  
logger.debug("fileFileName is :" + FileName);
                  
// 判断文件是否有后缀名
                  
int index = FileName.lastIndexOf(".");
                  
if (index != -1) {
                           saveAsName+=FileName.substring(index);
                  
}
                  
FileInputStream fis = null;
                  
try {
                           fis = new FileInputStream(file);
                  
} catch (FileNotFoundException e1) {
                           // TODO Auto-generated catch block
                           logger.error("文件上传时:找不到指定上传文件!");
                           return false;
                  
}
                  
FileOutputStream fos = null;
                  
try {
                           fos = new FileOutputStream(new File(Dir, saveAsName));
                           logger.debug("Dir:       " + Dir);
                  
         logger.debug("SaveAsFileName:    "+saveAsName);
                  
} catch (FileNotFoundException e1) {
                           // TODO Auto-generated catch block
                           e1.printStackTrace();
                           logger.error("文件上传时:找不到指定目标文件!");
                           return         false;
                  
}
 
                  
byte[] buffer = new byte[400];
                  
int length = 0;
                  
try {
                           while ((length = fis.read(buffer)) > 0) {
                                    fos.write(buffer, 0, length);
                           }
                  
} catch (IOException e1) {
                           // TODO Auto-generated catch block
                           e1.printStackTrace();
                           logger.error("IO错误!");
                           return false;
                  
}
 
                  
try {
                           fis.close();
                           fos.close();
                  
} catch (IOException e1) {
                           // TODO Auto-generated catch block
                           e1.printStackTrace();
                           logger.error("关闭IO流错误!");
                           return         false;
                  
}
                  
return true;
         }
 
}
      

       在点聚信息技术有限公司提供的示例程序里面,在接收端是用jsp写的,里面调用了SmartUpload这个对象以及其方法,在这里我们用Struts框架中的Action代替掉了jsp文件,并且脱离了SmartUpload初始化时需要获取pageContext这个对象的烦恼。提供了了Weboffice向服务器传输文件的功能。

       参考链接:http://www.dianju.cn/forum/viewtopic.php?f=2&t=2

前端编辑文件,并且点击提交上传

在服务器端收到该文件并另存为test.doc
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息