泛微ECOLOGY9实现在不登录状态根据文档ID调用文档的接口实现
FileServlet.java
package com.file.servlet;
import com.caucho.util.Sprintf;
import org.apache.tools.ant.taskdefs.EchoXML;
import weaver.conn.RecordSet;
import weaver.file.ImageFileManager;
import weaver.general.BaseBean;
import weaver.general.Util;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
public class FileServlet extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("application/x-zip-compressed;charset=utf-8"); // 获取http传过来的参数
String docid = request.getParameter("fileid"); //获取文档id
String method = "";
Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
try {
if (!pattern.matcher(docid).matches()) {
docid = com.api.doc.detail.util.DocDownloadCheckUtil.DncodeFileid(docid);
}
} catch (Exception e) {
e.printStackTrace();
} // 将文件夹download打包到文件夹downloadzip
RecordSet rs = new RecordSet();
rs.execute(" select docimagefile.imagefileid as imagefileid,filerealpath,imagefiletype,imagefile.imagefilename,iszip from imagefile,docimagefile "
+ " where imagefile.imagefileid =docimagefile.imagefileid and docimagefile.docid='" + docid + "'");
if (rs.first()) { // 设置http的头部,可设置下载的zip的文件名
response.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(rs.getString("imagefilename"), "utf-8") + "\"");
String filerealpath = rs.getString("filerealpath");
String imagefileid = rs.getString("imagefileid");
int iszip = rs.getInt("iszip");
new BaseBean().writeLog("iszip==>" + iszip + ",filerealpath=====>" + filerealpath);
ZipFile zf = null;
ZipInputStream zin = null;
ZipEntry ze = null;
InputStream is = null;
File file = null;
try {
if (1 == iszip) {
new BaseBean().writeLog("压缩文件");
zf = new ZipFile(filerealpath);
zin = new ZipInputStream(new BufferedInputStream(new FileInputStream(filerealpath)));
ze = zin.getNextEntry();
is = zf.getInputStream(ze);
} else {
ImageFileManager imageFileManager = new ImageFileManager();
imageFileManager.getImageFileInfoById(Util.getIntValue(imagefileid, 0));
is = imageFileManager.getInputStream();
new BaseBean().writeLog("非压缩文件");
}
// URLEncoder.encode(rs.getString("imagefilename"), "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
byte[] buf = new byte[4096]; /* 创建输出流 */
ServletOutputStream servletOS = response.getOutputStream();
int readLength;
while (((readLength = is.read(buf)) != -1)) {
servletOS.write(buf, 0, readLength);
}
servletOS.flush();
servletOS.close();
}
}
}
1、将以上代码编译后放到WEAVER\ecology\classbean\com\file\servlet目录中。
2、然后修改WEAVER\ecology\WEB-INF目录下的web.xml文件
在文件尾部上面添加
<!--文档附件读取Start lth-->
<servlet>
<servlet-name>FileServlet</servlet-name>
<servlet-class>com.file.servlet.FileServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FileServlet</servlet-name>
<url-pattern>/file/fileServlet</url-pattern>
</servlet-mapping>
<!--文档附件读取Start end-->
3、重启OA服务后,就可以通过/file/fileServlet配置的路径访问文件了。
如:http://OA地址/file/fileServlet?fileid=<%=82413" 82413为docid。
参考资料来源:
https://gitee.com/L1uTaihong/WeaverEc9CustomDev/blame/master/src/com/customization/servlet/FileServlet.java
使用说明 http://note.youdao.com/s/77miwS0Q