⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 downloadman.java

📁 用mina框架编写的转发代理服务程序,含自动更新功能
💻 JAVA
字号:
package com.frontMachine.client;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.Socket;

import org.apache.log4j.Logger;

import com.frontMachine.server.Files4Download;
import com.frontMachine.server.Files4ICReaderSys;
import com.frontMachine.server.Files4PosSys;
import com.frontMachine.setting.RemoteHost;
import com.frontMachine.setting.ServerSetting;
import com.frontMachine.util.ExceptionLoger;
import com.frontMachine.util.StrUtil;

/**
 * @下载控制类 从服务器下载需要更新的文件到相应的目录下
 * @author LRT
 * @version 1.0
 */
public class DownloadMan 
{
	private Socket client;
	private RemoteHost rmh;
	private InputStream clientIn;
	private BufferedReader br;
	private DataOutputStream out;
	private int downloadBufSize=2048; 
	private int downloadType;
	private Files4Download f4dl;
	private static Logger loger = Logger.getLogger(DownloadMan.class);
		
	
	public DownloadMan(RemoteHost rmh ,int downloadType )
	{
		this.rmh=rmh;
		downloadBufSize=ServerSetting.getInstance().getDownloadBufSize();
		if(downloadType == DownloadType.DOWNLOADTYPE_POS )
		{
			this.f4dl = Files4PosSys.getInstance();
			this.downloadType = downloadType;
		}
		else if(downloadType == DownloadType.DOWNLOADTYPE_ICREADER)
		{
			this.f4dl = Files4ICReaderSys.getInstance();
			this.downloadType = downloadType;
		}
		else
		{
			ExceptionLoger.ExLog("::DownloadMan:下载类别不存在!!");
		}	
	}
	
	public void connectSocket() throws Exception
	{
		client=new Socket(rmh.getIp(),rmh.getPort());
		client.setSoTimeout(10000);
		clientIn=client.getInputStream();
		br = new BufferedReader(new InputStreamReader(clientIn));
		out = new DataOutputStream(client.getOutputStream());
		
	}
	
	private String requestFileMsg(int index) throws IOException
	{
		//System.out.println("request file "+index);
		out.write(("<filename>"+ index +"</filename><downloadtype>"+downloadType+"</downloadtype>\r\n").getBytes());
		out.flush();
		String rtn=br.readLine();	
		return rtn;		
	}

	private void downloadData(InputStream in,String fileName,long fileSize)
	{
		try
		{
			long sTime=System.currentTimeMillis();
			File dlf = new File(f4dl.getPath()+File.separator+fileName);
			//System.out.println("path:"+f4dl.getPath());
			FileOutputStream fos=new FileOutputStream(dlf,true);
			byte[] buff=new byte[downloadBufSize];
			long recLen=0;
			while(recLen<fileSize)
			{
				int iRead = in.read(buff);
				fos.write(buff,0,iRead);
				recLen+=iRead;
			}
			
			loger.info("下载文件<"+fileName+">到目录["+f4dl.getPath()+"],耗时"+(System.currentTimeMillis()-sTime)+"毫秒");
			fos.close();
		}catch(Exception e)
		{
			ExceptionLoger.ExLog(e);
		}
	}
	
	public void dispose()
	{
		try
		{
			this.connectSocket();
			String fileMsg0=this.requestFileMsg(0);
			if(StrUtil.showNull(fileMsg0).equals(""))
			{
				System.out.println("DownloadMan::服务器["+rmh.toString()+"]无文件信息返回...");
				return;
			}
			//System.out.println("----"+fileMsg0);
			if(StrUtil.getXMLData(fileMsg0,"updateable").equals("false"))
				return;
			
			String edition=StrUtil.getXMLData(fileMsg0,"edition");
			if(edition.equals(ServerSetting.getInstance().getEdition(downloadType)))
				return;
			
			int t = 0;
			while(f4dl.isLocked()||f4dl.isUsing())  //如果下载的目录锁定或正在使用时,进入等待,最多等4秒
			{
				try{
					t++;
					Thread.sleep(100);		
				}catch(Exception ex)
				{
					ExceptionLoger.ExLog(ex);
				}
				if(t>=40)
				{
					ExceptionLoger.ExLog("DownloadMan::dispose:下载目录已锁或正在使用.....");
					return;
				}
			}
			
			synchronized (f4dl)
			{
				try{
					f4dl.lockDIR();
					int filesNum=Integer.valueOf(StrUtil.getXMLData(fileMsg0,"filemax")).intValue();
					if(!f4dl.delAll())
					{
						ExceptionLoger.ExLog("更新下载目录前的删除文件操作失败,更新操作终止!");
						return;
					}		
					DataInputStream dis=new DataInputStream(this.clientIn);
					//下载第一个文件
					out.write(("<file>0</file><downloadtype>"+downloadType+"</downloadtype>\r\n").getBytes());
					out.flush();
					this.downloadData(dis, StrUtil.getXMLData(fileMsg0,"filename"),Long.parseLong(StrUtil.getXMLData(fileMsg0,"filelen")));
					//下载剩下的文件
					for(int i=1;i<filesNum;i++)
					{
						String fileMsg=this.requestFileMsg(i);
						//System.out.println(fileMsg);
						out.write(("<file>"+i+"</file><downloadtype>"+downloadType+"</downloadtype>\r\n").getBytes());
						out.flush();
						this.downloadData(dis, StrUtil.getXMLData(fileMsg,"filename"),Long.parseLong(StrUtil.getXMLData(fileMsg,"filelen")));
					}
					ServerSetting.getInstance().setEdition(edition,downloadType);
				}
				finally
				{
					//System.out.println("------unlock-----");
					f4dl.unlockDIR();
				}
			}
		}
		catch(ConnectException cone)
		{
			System.out.println("DownloadMan::连接服务器["+rmh.toString()+"]失败,不能下载更新文件...");
			//cone.printStackTrace();
		}
		catch(Exception e)
		{
			ExceptionLoger.ExLog("Catch Exception In DownloadMan~~~~");
			ExceptionLoger.ExLog(e);	
		}
		finally
		{
			try{
				out.close();
				br.close();
				client.close();
			}catch(Exception ex){}
		}
	}
	
	public static void main(String[] args) throws Exception //test
	{
		DownloadMan dlm=new DownloadMan(new RemoteHost("192.168.0.80",30508,"test"),DownloadType.DOWNLOADTYPE_POS);
		
		dlm.dispose();
		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -