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

📄 files4download.java

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

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;

import com.frontMachine.setting.ServerSetting;
import com.frontMachine.util.ExceptionLoger;
import com.frontMachine.util.FileDeleteFailException;

/**
 * @下载目录类 定义被下载文件的目录和一些方法
 * @author LRT
 * @version 1.0
 */

public abstract class Files4Download 
{
	//protected static Files4Download file4dlInstance;
	protected  String dlpath; //下载目录的路径
	protected  File[] dlfiles; //下载目录下的文件
	protected  File dldir = new File(File.separator); //下载目录	
	protected  int downloadType; //下载类型 pos系统的目录和ic读写器系统的目录 	
	protected  boolean lockedFlag = false;
	protected  boolean usingFlag = false;
	/*private Files4Download()
	{
		dldir=new File(dlpath);
		try
		{
			System.out.println("The Directory For Download:  "+dldir.getCanonicalPath());
		}
		catch(Exception e){}
		this.reflashDIR();
	}
	public static synchronized Files4Download getInstance()
	{
		try
		{
			if(file4dlInstance == null) 
			{
				file4dlInstance = new Files4Download();
			}
		}
		catch (Exception e)
		{
			ExceptionLoger.ExLog(e);
		}
		return file4dlInstance;
	}*/
	//public abstract String getPath();
	
	
	public synchronized void reflashDIR() //刷新下载目录
	{
		/*String[] fileNames=dldir.list(new FilenameFilter()
				{
					public boolean accept(File dir,String name)
					{
						return new File(dir+File.separator+name).isDirectory()==false;
					}
				});
		
		dlfiles=new File[fileNames.length];
		
		for(int i=0;i<fileNames.length;i++)
		{
			dlfiles[i]=new File(dldir.getAbsolutePath()+File.separator+fileNames[i]);
		}*/
		dlfiles=dldir.listFiles(new FilenameFilter() //listFiles方法直接返回符合过滤条件的File[]
		{
			public boolean accept(File dir,String name)
			{
				return new File(dir+File.separator+name).isDirectory()==false;
			}
		});
	}
	
	
	public synchronized boolean delAll()
	{
		try{	
			this.delALL(dldir);
			return true;
		}catch(FileDeleteFailException fdfe)
		{
			ExceptionLoger.ExLog(fdfe);
			return false;
		}
		catch(Exception ex)
		{
			ExceptionLoger.ExLog(ex);
			return false;
		}
		
	}
	
	
	public synchronized void delFiles()
	{
		for(int i=0;i<dlfiles.length;i++)
			dlfiles[i].delete();	
	}
		
	
	public  void delALL(File f) throws FileDeleteFailException,Exception
	{
		File[] subF=f.listFiles();
		for(int i=0;i<subF.length;i++)
		{
			
			if(subF[i].isDirectory())
				delALL(subF[i]);
			
			boolean hasDel=subF[i].delete();
			if(!hasDel)
				throw new FileDeleteFailException("文件"+subF[i].toString()+"删除失败!");
		}
		//f.delete();	
	}
	
	public synchronized void lockDIR()
	{
		lockedFlag=true;
	}
	
	public synchronized void unlockDIR()
	{
		lockedFlag=false;
	}
	
	public boolean isLocked()
	{
		return lockedFlag;
	}
	
	public synchronized void useDIR()
	{
		usingFlag=true;
	}
	
	public synchronized void unuseDIR()
	{
		usingFlag=false;
	}
	public boolean isUsing()
	{
		return usingFlag;
	}
	
	public int getPartsNum(File f,int partsize)
	{
		if(f!=null)
		{
			return (int)((f.length()+partsize-1)/partsize);
		}
		return -1;
	}
	
	public File[] getDlfiles()
	{
		return dlfiles;
	}
	
	public String getPath()
	{
		return this.dlpath;
	}
	
	public String[] fileMsgPack()     //返回目录下的文件信息
	{
		if(dlfiles==null)
			return null;
		String[] dlfMsgPack=new String[dlfiles.length];
		ArrayList dlfMsgPackList=new ArrayList();
		for(int i=0;i<dlfiles.length;i++)
		{
			dlfMsgPackList.add("<filename>"+dlfiles[i].getName()+"</filename>"+"<filelen>"+dlfiles[i].length()+"</filelen>"+"<filemax>"+dlfiles.length+"</filemax>"+"<edition>"+ServerSetting.getInstance().getEdition(downloadType)+"</edition>");
		}
		return (String[])dlfMsgPackList.toArray(dlfMsgPack);
	
		/*	List提供了toArray()的方法,但是要使用不好,就会有ClassCastException
		究竟这个是如何产生的,且看代码:
		-----------------------------------------------------------------------------------
		        List list = new ArrayList();
		        list.add(new Long(1));list.add(new Long(2));
		        list.add(new Long(3));list.add(new Long(4));
		        Long[] l = (Long[])list.toArray();
		        for(int i=0; i<l.length; i++)
		            System.out.println(l[i].longValue());
		-----------------------------------------------------------------------------------
		会抛java.lang.ClassCastException。
		下面是ArrayList的两个toArray()方法的源代码:
		-----------------------------------------------------------------------------------
		public Object[] toArray() 
		{ 
		   Object[] result = new Object[size];    
		   System.arraycopy(elementData, 0, result, 0, size);    
		   return result;
		}
		
		public Object[] toArray(Object a[]) 
		{    
		 if (a.length < size)        
		 a = (Object[])java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), size);     
		 System.arraycopy(elementData, 0, a, 0, size);    
		 if (a.length > size)        
		 a[size] = null;    
		 return a;
		}
		-----------------------------------------------------------------------------------
		可以看出,不带参数的toArray方法,是构造的一个Object数组,然后进行数据拷贝,此时进行转型就会产生ClassCastException
		而带参数的toArray方法,则是根据参数数组的类型,构造了一个对应类型的,长度跟ArrayList的size一致的空数组,虽然方法本身
		还是以Object数组的形式返回结果,不过由于构造数组使用的ComponentType跟需要转型的ComponentType一致,就不会产生转型异常 
		正确的方式
		1. Long[] l = (Long []) list.toArray(new Long[0]);
	
		2. Long [] a = new Long[<total size>];
		   Long [] l = (Long []) list.toArray(a);
		2要注意的是:你要是传入的参数为9个大小,而list里面有5个object,那么其他的四个很可能是null , 使用的时候要注意。*/
	}    
	
	
	public static void main(String[] args) //test
	{
		/*try{
			File file4del=new File("C:\\del");
		//Files4Download.getInstance().delFiles();
		Files4Download.getInstance().delALL(file4del);
		}catch(Exception e)
		{
			e.printStackTrace();
		}*/
	}
	
	
}

⌨️ 快捷键说明

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