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

Domino Java代理实现Ftp上传、下载

2014-07-11 17:56 330 查看
之前的博客描述了在windows平台下,通过Lotusscript方式实现F图片上传、下载,但是在linux环境下,没找到lotusscirpt方法,是通过java方式。

/article/9130091.html

java的实现方式网上很多,我把现在项目用的代码分享出来,参考代码如下:

import lotus.domino.*;
import java.io.*;
import java.util.StringTokenizer;
import java.util.ArrayList;
import java.util.Vector;

import sun.net.*;
import sun.net.ftp.*;

public class JavaAgent extends AgentBase {

//	private FtpClient ftpclient;

/**
* 在FTP服务器上建立指定的目录,当目录已经存在的情下不会影响目录下的文件,这样用以判断FTP
* 上传文件时保证目录的存在目录格式必须以"/"根目录开头
* @param pathList String * @throws Exception
**/

public void NotesMain() {
System.out.println("ftp —— start");
Database db = null;
Document doc = null; //要处理的应用文档
Database curDB = null ; //当前数据库
Agent agent = null; //当前代理
Database ftpProFileDb = null; //本服务器ftp配置文档所在数据库
Document ftpProFileDoc = null; //本服务器的ftp配置文档
View ftpView = null; //寻找组织ftp配置文档的视图
Vector sM = null; //存放附件名的多值域的值的数组

try {
Session session = getSession();
AgentContext agentContext = session.getAgentContext();
// (Your code goes here)

String ftpUrl = "IP地址";
int ftpPort = 21;
String ftpUserName = new String("cpmis");
String ftpPassWord = new String("cpmis");
FtpClient ftpclient = null;

System.out.println("----连接FTP服务器。开始------");
ftpclient = new FtpClient(ftpUrl, ftpPort);
System.out.println("----连接FTP服务器。成功------");

System.out.println("----登陆FTP服务器。开始------");
ftpclient.login(ftpUserName, ftpPassWord);
System.out.println("----登陆FTP服务器。成功------");

String ftpPath;
String filepathSource;
String filepathTarget;
String fileName;

curDB = agentContext.getCurrentDatabase();

ftpProFileDb = null;
ftpProFileDoc = null;
ftpPath = curDB.getFilePath();
//ftpPath = ftpPath.replace("\\", "/");
ftpPath = MyStringClass.replaceAll(ftpPath, "\\", "/");
ftpPath = ftpPath.substring(0,ftpPath.indexOf("/"));
ftpPath = ftpPath + "/system/AppSystem";

ftpProFileDb = session.getDatabase(null, ftpPath);
if (!ftpProFileDb.isOpen())
{
System.out.println("应用资源库AppSystem.nsf does not open ");
return;
}

ftpView = ftpProFileDb.getView("FTPInfo");
if (ftpView==null)
{
System.out.println("应用资源库AppSystem.nsf中视图ftpView未找到");
return;
}

ftpProFileDoc = ftpView.getFirstDocument();
if (ftpProFileDoc==null)
{
System.out.println("应用资源库AppSystem.nsf中ftp配置文档未找到");
return;
}

ftpPath = getItemValueStr("FtpPath",ftpProFileDoc);
//ftpPath = ftpPath.replace("\\", "/");
ftpPath = MyStringClass.replaceAll(ftpPath, "\\", "/");
if(ftpPath.length()!=0&&!ftpPath.endsWith("/"))
ftpPath = ftpPath + "/";

if(ftpPath.length()==0)
ftpPath = "/notes/ftpdoc/";

db = session.getDatabase(null, "chtweboa/exchange/cpmisoa");
if (!db.isOpen())
{
System.out.println("CPMIS中间库cpmisoa.nsf does not open");
return;
}

agent = agentContext.getCurrentAgent();

doc = db.getDocumentByID(agent.getParameterDocID());

if(doc!=null)
{
sM = doc.getItemValue("SRMAttachments");
for(int i=0;i<sM.size();i++)
{
fileName = ((String)(sM.elementAt(i)));
if(fileName.length()==0)
continue;

//filepathSource = "D:/Lotus/Domino/ftpdoc/" + getItemValueStr("ftpTagThis",doc) + "/"  + fileName;
filepathSource = ftpPath + getItemValueStr("ftpTagThis",doc) + "/"  + fileName;
filepathTarget = "oareplay/" + getItemValueStr("ftpTagTo",doc) + "/" + fileName;

System.out.println("上传文件开始--"+filepathSource);
File tempFile = new File(filepathSource);
if(tempFile.exists() && tempFile.isFile())
{
copy(ftpclient, filepathSource, filepathTarget, fileName);
}
else
{
System.out.println("文件【"+filepathSource+"】不存在或不是文件!");
}
tempFile = null;
System.out.println("上传文件结束--"+filepathSource);
}

System.out.println("--退出FTP服务器。开始--");
ftpclient.sendServer("QUIT\r\n");
System.out.println("--退出FTP服务器。成功--");
}
else
{
System.out.println("doc no found!");
}

}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if(db!=null)
{
db.recycle();
}
if(doc!=null)
{
doc.recycle();
}
if(curDB!=null)
{
curDB.recycle();
}
if(agent!=null)
{
agent.recycle();
}
if(ftpProFileDb!=null)
{
ftpProFileDb.recycle();
}
if(ftpProFileDoc!=null)
{
ftpProFileDoc.recycle();
}
if(ftpView!=null)
{
ftpView.recycle();
}
if(sM!=null)
{
sM = null;
}
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.println("ftp —— end");
}
}

public String getItemValueStr(String itemName,Document doc)
{
if(itemName.length()==0)
return "";
if(doc==null)
return "";

String temp = "";
Item item = null;

try
{
item = doc.getFirstItem(itemName);
if(item==null)
return "";
else
temp = item.getText();
}
catch(Exception e)
{
temp = "";
e.printStackTrace();
}

return temp;
}

public static void buildList(FtpClient ftpclient, String pathList) throws Exception
{
ftpclient.ascii();
StringTokenizer s = new StringTokenizer(pathList, "/");
String pathName = "";
while (s.hasMoreElements()) {
pathName = pathName + "/" + (String) s.nextElement();
try {
ftpclient.sendServer("XMKD " + pathName + "\r\n");
}
catch (Exception e) {
e = null;
}
int reply = ftpclient.readServerResponse();
}
ftpclient.binary();
}

public static void copy(FtpClient ftpclient, String pathSource, String pathTarget, String filename) throws Exception
{
File file = new File(pathSource);
FileInputStream in = new FileInputStream(pathSource);

String destination = pathTarget;
byte[] sourceData = new byte[(int) file.length()];
buildList(ftpclient, destination.substring(0, destination.lastIndexOf("/")));
ftpclient.binary(); //此行代码必须放在buildList之后
TelnetOutputStream out = ftpclient.put(destination);

try{
byte[] buff = new byte[(int) file.length()];
int len = 0;
//			while ((len = in.read(buff)) != -1) {
while ((len = in.read(buff)) > 0) {
out.write(buff, 0, len);
}
}
finally{
try{
if (out != null){
out.close();
}
}
catch (IOException ioe){
//ignore
ioe.printStackTrace();
}
}
}

public void downFile(FtpClient ftpclient,String SourceFileName, OutputStream stream) throws Exception
{
ftpclient.binary();
TelnetInputStream ftpIn = ftpclient.get(SourceFileName);
byte buf[] = new byte[0x32000];
for(int bufsize = 0; (bufsize = ftpIn.read(buf, 0, buf.length)) != -1;){
stream.write(buf, 0, bufsize);
}
stream.flush();
}

public static boolean fileCopy(FtpClient ftpclient, String filePathSource, String filePathTarget)
{
try {
/** * filePathSource:  源文件夹路径 * **/
/** * filePathTarget:目标文件夹路径 * **/
File filePSource = new File(filePathSource);	//获取源文件夹路径
String[] fileListSource = filePSource.list();
long lngSource = fileListSource.length;
//			lngSource = 2;
for (int i = 0; i < lngSource; i++) {
File fileSource = new File(filePathSource + fileListSource[i]);
if (!fileSource.isDirectory()) {
String fullFileNameSource = fileSource.getAbsolutePath();
String fullFileNameTarget = filePathTarget + fileSource.getName();
copy(ftpclient, fullFileNameSource, fullFileNameTarget, fileSource.getName());	//拷贝文件
}
else{
String subFilePathSource = filePathSource + fileListSource[i] + File.separator;
String subFilePathTarget = filePathTarget + fileListSource[i] + File.separator;
File subFilePTarget = new File(subFilePathTarget);
if (!subFilePTarget.exists()){
subFilePTarget.mkdir();
}
fileCopy(ftpclient, subFilePathSource, subFilePathTarget);
}
}
}
catch (Exception e) {
e.printStackTrace();
}
return true;
}
public static boolean ftpUpload21(String url, int port, String user, String password, String filepathlocal, String filepathremote)
{

System.out.println("IP> " + url + " 端口> " + port + " 用户> " + user);
System.out.println("源文件夹> " + filepathlocal + " 目标文件夹> " + filepathremote );
try {
System.out.println("----连接FTP服务器。开始------");
FtpClient ftpclient = new FtpClient(url, port);
System.out.println("----连接FTP服务器。成功------");

System.out.println("----登陆FTP服务器。开始------");
ftpclient.login(user, password);
System.out.println("----登陆FTP服务器。成功------");

System.out.println("---上传文件。开始---");
if (fileCopy(ftpclient, filepathlocal, filepathremote)){
System.out.println("文件拷贝完成!");
}
System.out.println("---上传文件。成功---");

//			System.out.println("--退出FTP服务器。开始--");
ftpclient.sendServer("QUIT\r\n");
//			System.out.println("--退出FTP服务器。成功--");

} catch (Exception e) {
e.printStackTrace();
}
return true;
}
}

class MyStringClass
{
public static String replaceAll(String SourceStr,String subStr,String newSubStr)
{
if(SourceStr.length()==0 ||subStr.length()==0)
{
return SourceStr;
}
if(subStr.equals(newSubStr))
{
return SourceStr;
}
if(SourceStr.length()<subStr.length())
{
return SourceStr;
}

String tempStr = SourceStr;
int position = -1;
int subStrLength = subStr.length();
position = tempStr.indexOf(subStr);
if(position==-1)
{
return SourceStr;
}

while(position!=-1)
{
if(position+subStrLength==tempStr.length())
{
tempStr = tempStr.substring(0,position) + newSubStr;
}
else
{
tempStr = tempStr.substring(0,position) + newSubStr + tempStr.substring(position+subStrLength);
}
position = tempStr.indexOf(subStr);
}

return tempStr;
}
public static String replaceFirst(String SourceStr,String subStr,String newSubStr)
{
if(SourceStr.length()==0 ||subStr.length()==0)
{
return SourceStr;
}
if(subStr.equals(newSubStr))
{
return SourceStr;
}
if(SourceStr.length()<subStr.length())
{
return SourceStr;
}

String tempStr = SourceStr;
int position = -1;
int subStrLength = subStr.length();
position = tempStr.indexOf(subStr);
if(position==-1)
{
return SourceStr;
}

if(position+subStrLength==tempStr.length())
{
tempStr = tempStr.substring(0,position) + newSubStr;
}
else
{
tempStr = tempStr.substring(0,position) + newSubStr + tempStr.substring(position+subStrLength);
}

return tempStr;
}
}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: