发帖    主题    评论    推荐    标签    作者    订阅    查搜    注册   登陆   关注
 
面向对象 设计模式 领域驱动设计 企业架构 框架 开发教程 微服务 CQRS 扩展性 并发编程 事件溯源 分布式 SOA

我是想不开了,谁能想开?帮帮忙吧。

2005-01-09 14:40
赞助商链接

程序一每隔30分钟调用程序二。程序二的功能是把outdata文件夹下的文件上传到ftp服务器。单独执行程序二没有问题,但是,用程序一调用程序二,就出现这样的问题,每次循环只上传两个文件,非常美名奇妙。请高手赐教。

程序一源码
package ymisftp;

import java.awt.*;
import java.io.*;
import java.lang.*;
import java.sql.*;
import java.text.*;
import java.util.*;
import java.util.StringTokenizer;
import java.net.*;

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/

public class ymisftp {
private final int minutes;
private final Timer timer = new Timer();
Runtime r=Runtime.getRuntime();
Process p=null;
private static Properties appProps;

//Construct the application
public ymisftp(int minutes) {
this.minutes = minutes;
}

public void start() {

timer.schedule(new TimerTask() {
public void run() {
System.out.println(minutes);
try {
if(p!=null){
p.destroy();
}
p=r.exec("d:\\deffil\\ymisftpapp.exe");
}
catch (Exception e) {
System.out.println(e.toString());
}
}
}, 0, 30 * 60 * 1000);
}

public static String getProperty(String key) {
if (appProps == null) { // Lazy construction and singleton
try {
appProps = new Properties();
appProps.load(new FileInputStream(new File(
"d:\\deffil\\log4j.properties")));

}
catch (IOException e) {
// todo: write to log
System.out.println(e.toString());
}
}
return appProps.getProperty(key);
}

//Main method
public static void main(String[] args) {
String s=ymisftp.getProperty("transtimer");
ymisftp eggTimer = new ymisftp(Integer.parseInt(s));
eggTimer.start();
}
}

程序二源码,编译后d:\\deffil\\ymisftpapp.exe
package ftp;

import sun.net.ftp.*;
import sun.net.*;
import java.applet.*;
import java.io.*;
import java.io.IOException;
import java.util.StringTokenizer;
import sun.net.ftp.FtpClient;
import java.util.ArrayList;
import java.util.Properties;
import java.util.*;
import java.lang.*;
import javax.swing.*;
import java.net.*;
import ymis.dblink;
import org.apache.log4j.*;
import java.sql.*;

public class ftp {
FtpClient aftp;
DataOutputStream outputs ;
TelnetInputStream ins;
TelnetOutputStream outs;
private final static String APP_PROPERTIES = "app.properties";
private static Properties appProps;
private final java.util.Timer timer = new java.util.Timer();
private final int minutes;
dblink dblink = new dblink();
ResultSet rs = null;
ArrayList al = new ArrayList();
String localip = "";

int ch;
public String a;
String hostname="";
private String path = "/";

//Construct the application
public ftp(int minutes) {
this.minutes = minutes;

}

public static String getProperty(String key) {
if (appProps == null) { // Lazy construction and singleton
try {
appProps = new Properties();
appProps.load(new FileInputStream(new File(
"d:\\deffil\\log4j.properties")));

}
catch (IOException e) {
// todo: write to log
log("D:\\ymisdata\\",e.getMessage());
}
}
return appProps.getProperty(key);
}

//Main method
public static void main(String[] args) {
String hostname = ftp.getProperty("ftpserverip");
String port = ftp.getProperty("ftpserverport");
String uid = ftp.getProperty("ftpuid");
String pwd = ftp.getProperty("ftppwd");
String RWFileDir = "D:\\ymisdata\\"; //文件目录
String sdir = RWFileDir + "outdata\\";
File fdir = new File(sdir);
String FilName = "";
String FileName = "";
String s = "";
String lsbl = "";
int wjnum = 0;

//连接ftp服务器
ftp ft = new ftp(99);
ft.connect(RWFileDir, hostname, Integer.parseInt(port), uid, pwd);

if (ft.aftp != null) {
//上传文件
log(RWFileDir, "上传文件");
for (int i = 0; i < fdir.list().length; i++) {
s = (fdir.list());
File f = new File(sdir, s);
lsbl = sdir + s;
if (f.exists()) {
ft.uploadFile(RWFileDir, lsbl);
f.delete();
}
}
}

//断开服务器连接
log(RWFileDir, "断开服务器连接");
ft.stop(RWFileDir);
}
public FtpClient connect(String RWFileDir,String hostname,int port,String uid,String pwd)
{
this.hostname = hostname;
System.out.println("正在连接"+hostname+",请等待.....");
try{
aftp = new FtpClient(hostname,port);
aftp.login(uid,pwd);
aftp.binary();
//aftp.openPortDataConnection();
a = "连接主机:"+hostname+"成功!";
System.out.println(a);
}
catch(FtpLoginException e){
a="登陆主机:"+hostname+"失败!请检查用户名或密码是否正确:"+e;
System.out.println(a);
//return false;
}
catch(FtpProtocolException e)
{
a="连接主机:"+hostname+"超时!网络状况不良。"+e;
System.out.println(a);
//return false;
}
catch (IOException e){
a="连接主机:"+hostname+"失败!请检查端口是否正确:"+e;
System.out.println(a);
//return false;
}
catch(SecurityException e)
{
a="无权限与主机:"+hostname+"连接!请检查是否有访问权限:"+e;
System.out.println(a);
//return false;
}

log(RWFileDir,a);
return aftp;
}

public void stop(String RWFileDir)
{
String message = "";
try {
if(aftp!=null){
aftp.closeServer();
message = "与主机"+hostname+"连接已断开!";
//System.out.println(message);
log(RWFileDir,message);
}
}
catch(IOException e)
{
message = "与主机"+hostname+"断开连接失败!"+e;
//System.out.println(message);
log(RWFileDir,message);
}
}

public boolean downloadFile(String RWFileDir,String filepathname){
boolean result=true;
String message = "";
if (aftp != null)
{
System.out.println("正在下载文件"+filepathname+",请等待....");
//String badfile = filepathname.substring(filepathname.length()-4,filepathname.length());
//String badlog = filepathname.substring(filepathname.length()-7,filepathname.length());
//String baddir = "";
// if ((badfile.compareTo(".bad") != 0) && (badlog.compareTo(".badlog") != 0)){
// baddir = "subunsubtosp\\";
// }
// else{
String baddir = "indata\\";
// }
String strdir = "outdata\\";
//System.out.println(RWFileDir + baddir + filepathname);
try{
//FtpClient fc=new FtpClient("172.16.3.104",21);
//fc.login("lee","lee");
int ch;
File fi = new File(RWFileDir + baddir + filepathname);
//aftp.cd(strdir);
RandomAccessFile getFile = new RandomAccessFile(fi,"rw");
getFile.seek(0);
TelnetInputStream fget=aftp.get(strdir+filepathname);
DataInputStream puts = new DataInputStream(fget);
while ((ch = puts.read()) >= 0) {
getFile.write(ch);
}
//s.delete();
fget.close();
getFile.close();
//fc.closeServer();

message = "下载"+filepathname+"文件到"+baddir +"目录成功!";
//System.out.println(message);
log(RWFileDir,message);
}
catch(IOException e){
message = "下载"+filepathname+"文件到"+baddir +"目录失败!"+e;
//System.out.println(message);
log(RWFileDir,message);
result = false ;

}
}
else{
result = false;
}
return result;
}

public void copyFile(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { //文件存在时
InputStream inStream = new FileInputStream(oldPath); //读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
int length;
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
fs.write(buffer, 0, byteread);
}
fs.close();
inStream.close();
}
}
catch (Exception e) {
System.out.println("复制文件"+oldPath+"出错");
e.printStackTrace();

}
}

public boolean uploadFile(String RWFileDir,String filepathname){
boolean result = true;
String message = "";
if (aftp != null)
{
System.out.println("正在上传文件"+filepathname+",请等待....");

try{
int index = filepathname.lastIndexOf("\\");
int isoutdata=filepathname.lastIndexOf("\\outdata\\");
String filename = filepathname.substring(index+1);
File localFile = new File(filepathname) ;

RandomAccessFile sendFile = new RandomAccessFile(filepathname,"r");
//
sendFile.seek(0);
//改名上传temp_
//filename = filename.substring(0,15)+"temp_"+filename.substring(15,filename.length());
if(isoutdata>0){
outs = aftp.put("indata\\" + filename);
}else{
outs = aftp.put("messagestate\\" + filename);
}
outputs = new DataOutputStream(outs);
while (sendFile.getFilePointer() < sendFile.length() )
{
ch = sendFile.read();
outputs.write(ch);
}

outputs.close();
sendFile.close();

message = "上传"+filepathname+"文件成功!";
log(RWFileDir,message);
}
catch(IOException e){
// try {
// sendFile.close();
//}catch (IOException er) {
// log(RWFileDir,"发生错误时,关闭文件"+filepathname + "失败!" + er);
// }
message = "上传" + filepathname + "文件失败!" + e;
log(RWFileDir, message);
result = false;
}
}else{
result = false;
}
return result;
}

public static void deleFile(String RWFileDir,String FileName) {
//取得ReadFile目录下的文件
String sdir = RWFileDir + "indata\\";
File fdir = new File(sdir,FileName);
boolean del = fdir.delete();
}

// 返回当前目录的文件名称

public ArrayList getNameList(String PathName) throws IOException {
try {
BufferedReader dr = new BufferedReader(new InputStreamReader(aftp.
nameList(PathName)));
String s = "";
int pathlen = PathName.length();
while ( (s = dr.readLine()) != null) {
s = s.substring(pathlen, s.length());
isFile(s);
al.add(s);
//downloadFile(RWFileDir,s);
}
}
catch (IOException ie) {
log("D:\\ymisdata\\",ie.getMessage());
}
catch (SecurityException se) {
log("D:\\ymisdata\\",se.getMessage());
}
return al;
//System.out.println(al.add(s));
}

//判断服务器端文件是否存在
public boolean FilExist(String GotoFilName) throws IOException {
boolean result = false;
try {
BufferedReader dr = new BufferedReader(new InputStreamReader(aftp.
nameList("outdata\\")));
String s = "";
while ( (s = dr.readLine()) != null) {
s = s.substring(8, s.length());
isFile(s);
if (s.toUpperCase().equals(GotoFilName.toUpperCase())) {
result = true;
}
}
}
catch (IOException ie) {
log("D:\\ymisdata\\",ie.getMessage());
}
catch (SecurityException se) {
log("D:\\ymisdata\\",se.getMessage());
}
return result;
}

//判断服务器端文件是否存在
public boolean GotoFilExist(String GotoFilName) throws IOException {
boolean result = true;

BufferedReader dr = new BufferedReader(new InputStreamReader(aftp.nameList("outdata\\")));
String s = "";
while ( (s = dr.readLine()) != null) {
s = s.substring(8,s.indexOf("."));
isFile(s);
if(s.toUpperCase().equals(GotoFilName.toUpperCase())){
result = false;
//System.out.println("no delete:"+s+","+GotoFilName);
}
}
return result;
}

// 判断一行文件信息是否为目录

public boolean isDir(String line) {
return ( (String) parseLine(line).get(0)).indexOf("d") != -1;
}

public boolean isFile(String line) {
return!isDir(line);
}

// 处理getFileList取得的行信息

private ArrayList parseLine(String line) {
ArrayList s1 = new ArrayList();
StringTokenizer st = new StringTokenizer(line, " ");
while (st.hasMoreTokens()) {
s1.add(st.nextToken());
}
return s1;
}

//写消息日志
public static void log(String RWFileDir,String msg)
{
String message = "";
try {
java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.text.DateFormat dflog = new java.text.SimpleDateFormat("yyyyMMdd");
java.util.Date date = new java.util.Date() ;
String datestr = df.format(new java.util.Date()) ;
String datelog = dflog.format(new java.util.Date()) ;
//String datelog = datestr.substring(0,10);
//datelog = datelog.replace('-',' ');

//按日期每天生成一个日志文件
FileWriter fwl = new FileWriter(RWFileDir + "log\\CMSSftp"+datelog+".log",true);
PrintWriter outl = new PrintWriter(fwl);
outl.println(datestr + " " + msg);
outl.close();
fwl.close();
}catch (IOException e) {
message = "写log文件错误!"+e;
e.printStackTrace();
log(RWFileDir,message);
//System.out.println(message);
}
}
}

赞助商链接

赞助商链接

返回顶部

移动版 关于本站 使用帮助 联系管理员 最佳分辨率1366x768
OpenSource JIVEJDON Powered by JdonFramework Code © 2002-20 jdon.com