一、客户需求:
用户在搜索结果中对需要下载的文档进行选择,系统将选定的一批搜索结果批量下载到服务器上预定义好的目录,然后下载到用户自己的电脑中。
二、解决方案:
1、将选择的NOTE文档中的附件集中拆离到服务器上的指定目录中,
2、将指定的目录压缩;
3、返回链接给用户,用户自己下载保存。
三、程序代码:
import lotus.domino.*;
import java.io.*;
import java.util.*;
import java.io.File;
import org.apache.tools.zip.*;
import java.lang.NullPointerException;
public class CDGGzip
{
private File srcPath =null;
private String outFilename;
private int len;
private String filenames;
public void setSrcPath(String src){
srcPath=new File(src);
}
public File getSrcPath(){
return srcPath;
}
public void setOutFilename(String out){
outFilename=out;
}
public String getOutFilename(){
return outFilename;
}
//======删除文件夹
public static void deletefile(String delpath)
throws IOException,FileNotFoundException {
File file = new File(delpath);
if (!file.isDirectory()) {
file.delete();
}
else if (file.isDirectory()) {
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File delfile = new File(delpath + "\\" + filelist[i]);
if (!delfile.isDirectory())
delfile.delete();
else if (delfile.isDirectory())
deletefile(delpath + "\\" + filelist[i]);
}
file.delete();
}
}
//======
//压缩文件
备注:本段程序只做了对统一文件夹下的文件压缩,如果要对子目录压缩,还需要各位自己改改程序
public void gzip(){
byte[] buf = new byte[1024];
try {
len=srcPath.listFiles().length;
String[] filenames = new String[len];
outFilename=new String(srcPath+".zip");
File[] files = srcPath.listFiles();
for(int i=0;i<len;i++)
{
// if(files[i].isDirectory())
filenames[i]=srcPath.getPath()+File.separator+files[i].getName();
System.out.println("filenames"+filenames[i]);
}
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));
for (int i=0; i<filenames.length; i++)
{
FileInputStream in = new FileInputStream(filenames[i]);
out.putNextEntry(new org.apache.tools.zip.ZipEntry(files[i].getName()));
int len;
while ((len = in.read(buf)) > 0)
{
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
}
out.close();
}
catch (IOException e)
{
System.out.println(e);
}
}
}
=========================================
主程序:
import lotus.domino.*;
import java.io.*;
import java.util.Vector;
import java.util.Properties;
import java.util.Enumeration;
import java.lang.*;
import java.text.*;
import java.util.*;
public class JavaAgent extends AgentBase {
public void NotesMain() {
ReturnMessage returnMessage = new ReturnMessage();
PrintWriter mainPw = getAgentOutput();
String reMsg = "";
try {
Session session = getSession();
AgentContext agentContext = session.getAgentContext();
Database db = agentContext.getCurrentDatabase();
// Document pdoc = db.getProfileDocument("FM_DBSetting","");
//String curfilepath= pdoc.getItemValueString("F_Filepath");
String CurFilePath = db.getFilePath().replace(’\\’,’/’);
String directory = null;
directory = returnMessage.getDirectory(session);
int post = directory.indexOf(":\\");
String curpath=null;
String path=null;
if(post!=-1){ //Win
path = directory+"\\domino\\html\\DownloadsFolder\\";
}
else{
curpath = directory.replace(’\\’, ’/’);
path = "/"+curpath.substring(1,curpath.length())+"/domino/html/DownloadsFolder/";
}
View view = db.getView("VH_ByAllDoc");
Document CurDoc = agentContext.getDocumentContext();
String CurUserName = CurDoc.getItemValueString("F_CurUserName");
String CurFileName = path+"\\"+CurUserName+"\\";
String DownFileName = CurUserName+".zip";
String CurSelceted = CurDoc.getItemValueString("F_TmpSelected");
StringTokenizer st = new StringTokenizer(CurSelceted,"+"); //以+为分隔取字符串
while (st.hasMoreTokens()) //控制循环
{
String key=st.nextToken(); //获得+号第一个字符串
Document doc = view.getDocumentByKey(key,true);
if (doc!= null){
//SaveFile 创建文件夹,导出附件
String CurSubject = doc.getItemValueString("Subject");
RandomStringUtils randn = new RandomStringUtils();
String srcFile = CurSubject + randn.randomNumeric(8);
String filepath =path+"\\"+CurUserName+"\\";
File aFile=new File(filepath);
if (!aFile.exists())
{
aFile.mkdirs();
}
else{
new File(filepath).delete();
}
//提取附件名称,去除在线编辑WORD正文文件。
Vector v = session.evaluate("@AttachmentNames", doc);
int attno=v.size();
for (int i=0; i<v.size(); i++){
EmbeddedObject obj = doc.getAttachment((String)v.elementAt(i));
String curobj=(String)v.elementAt(i);
if (curobj.indexOf("LKSATT")<0)
{
obj.extractFile(filepath + (String)v.elementAt(i));
}
}
}
}
CDGGzip cdggzip=new CDGGzip();
cdggzip.setSrcPath(CurFileName);
cdggzip.gzip();
cdggzip.deletefile(CurFileName);
// reMsg ="文件测试";
reMsg ="请直接在下面的链接上按鼠标右键选择【目标另存为】[<br><a target=\"_blank\"
href=\"/DownloadsFolder/"+ DownFileName +"\">文件导出地址。</a>]";
returnMessage.returnMsg(reMsg, "B", mainPw, CurFilePath);
} catch(Exception e) {
e.printStackTrace();
}
}
}