java中用ftp实现每天凌晨1点自动上传,怎么会出现上传的文件会增多?
yudu 2009-11-25 11:12:01 任务:每天将数据库表中的一天的交易量以一个文本50000条记录上传到ftp服务器,一般5、6个文件是正常的,测试也是正常的,就是正常运行的时候变成了几十个文件了。请哪位大侠帮忙分析一下:
package ftp;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
public class FileFTP
{
private FtpClient ftpClient = null;
private List dateList = new ArrayList();
public void ftpFile()
{
try
{
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String date = simpleDateFormat.format(new Date());
System.out.println(date+"开始上传文件......");
uploadFile("d:/ XX");
System.out.println("文件已上传成功");
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void uploadFile(String path) throws IOException
{
try
{
Map map = (Map)readfile(path);
connectServer("0.000.0", "tt", "tt", "//" + path.substring(path.indexOf("/")+1));
Iterator iterator =map.keySet().iterator();
while(iterator.hasNext())
{
String key = iterator.next().toString();
String fileName = (String)map.get(key);
upload(path+"/"+fileName);
}
}
catch (Exception e)
{
System.out.println("上传" + path + "下的文件失败!" + e.getMessage());
}
finally
{
if (ftpClient != null)
{
ftpClient.closeServer();
}
}
}
@SuppressWarnings("unchecked")
private Map readfile(String filepath)
{
Map map = null;
try
{
map = new TreeMap();
File file = new File(filepath);
if (!file.isDirectory())
{
System.out.println(filepath + " 文件夹不存在!");
}
else if (file.isDirectory())
{
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++)
{
File readfile = new File(filepath + "/" + filelist[i]);
if (!readfile.isDirectory())
{
String fileName = readfile.getName();
long time = readfile.lastModified();
dateList.add(new Long(time));
map.put(String.valueOf(time), fileName);
}
}
}
}
catch (Exception e)
{
System.out.println("获取" + filepath + "下的文件失败!" + e.getMessage());
}
return map;
}
private void connectServer(String server, String user, String password, String path) throws IOException
{
ftpClient = new FtpClient();
ftpClient.openServer(server);
ftpClient.login(user, password);
if (path.length() != 0)
ftpClient.cd(path);
ftpClient.binary();
}
// 上传文件;并返回上传文件的信息
private void upload(String filename) throws Exception
{
TelnetOutputStream os = null;
FileInputStream is = null;
File file = null;
try
{
System.out.println("开始上传文件"+filename+"......");
file = new File(filename);
String tempFile = file.getName();// 获取上传文件的文件名
os = ftpClient.put(tempFile);
java.io.File file_in = new java.io.File(filename);
is = new FileInputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1)
{
os.write(bytes, 0, c);
}
}
finally
{
if (is != null)
{
is.close();
}
if (os != null)
{
os.close();
}
if(file!=null)
{
file.delete();
}
}
}
private void deleteFile(String fileName)
{
try
{
System.out.println("删除文件"+fileName+"......");
File file = new File(fileName);
file.delete();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
package thread;
import java.util.Calendar;
import data.DataAction;
import ftp.FileFTP;
public class AssistantThread implements Runnable
{
public void run()
{
System.out.println("次线程已启动......");
Calendar calendar = Calendar.getInstance();
if(calendar.get(Calendar.HOUR_OF_DAY)==9)
{
try
{
//生成文件
DataAction dataAction = new DataAction();
dataAction.createFile();
//上传文件
FileFTP ftp = new FileFTP();
ftp.ftpFile();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
package thread;
public class MainThread implements Runnable
{
public void run()
{
System.out.println("主线程启动......");
while(true)
{
try
{
Runnable r = new AssistantThread();
Thread t = new Thread(r);
t.start();
Thread.sleep(Long.parseLong("3600000"));//一个小时
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
package thread;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial")
public class ThreadServlet extends HttpServlet {
private Thread t =null;
public ThreadServlet()
{
super();
}
public void init() throws ServletException
{
Runnable r = new MainThread();
t = new Thread(r);
t.start();
}
public void destroy()
{
super.destroy();
if(t!=null)
{
t.interrupt();
t = null;
System.out.println("主线程销毁......");
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}