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

📄 fileprocessor.java

📁 该程序实现java语言对文件系统的统一处理
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*************************************************************************************************************
/*  名称/name:			文件处理支持类
/*  描述/description:	提供一个支持文件处理统一接口
/*  修改记录/revision:	
/*  日期			修改人			描述
*************************************************************************************************************/
import java.util.Vector;
import java.util.Enumeration;
import java.io.IOException;
import java.io.File;
import java.io.FileOutputStream;

public class FileProcessor {
	/** 错误信息 */
	private static final String READLINE_IOEXCEPTION = "读浏览器数据失败";
	private static final String CORRUPTED_INPUT_STREAM = "数据流非法";
 	private static final String FILE_OUTPUT_STREAM_IOEXCEPTION = "创建附件文件失败";
 	private static final String CLOSE_IOEXCEPTION = "关闭附件文件出错";
 	private static final String WRITE_IOEXCEPTION = "读附件文件出错";
 	private static final String WORKING_DIR_IS_FILE = "文件工作路径是一个文件";
 	private static final String WORKING_DIR_CREATE_ERROR = "不能创建文件工作路径";
 	private static final String REQUEST_TYPE_ERROR = "请求类型错误";
 	private static final String FILE_NOT_EXISTS = "文件不存在";
 	private static final String FILE_DELETE_ERROR = "文件删除错误";

 	/** HTTP流标记 */
 	private static final String MULTIDATATYPE="multipart/form-data";
 	private static final int BUF_SIZE = 409600;
 	private static final String NAME_STRING = "Content-Disposition: form-data; name=\"";
	private static final String FILENAME_STRING = "\"; filename=\"";
	private static final String CONTENT_TYPE_STRING = "Content-Type:";	

	/** HTTP流分析结果缓存 */
	protected Vector uploadParameterNames	= new Vector(0);
	protected Vector uploadSrcFileNames		= new Vector(0);
	protected Vector uploadFileNames		= new Vector(0);
	protected Vector uploadFileTypes		= new Vector(0);
	protected Vector uploadFileSizes		= new Vector(0);

	protected Vector parameterNames        = new Vector(0);
	protected Vector parameterValues       = new Vector(0);

	/** 文件上载属性 */

	/** 服务器端保存上载文件的物理根路径 */
	protected String rootPath = "";

	/** 服务器端保存上载文件的文件上载相对路径 */
	protected String relativeURLPath = "";
	protected String relativeFilePath = "" ;

	/** 是否保留上载文件的原始扩展名 */
	protected boolean holdOrgFileType = true;

	/** 如不保留上载文件的原始扩展名,文件上载后的新扩展名 */
	protected String fileType = "";

	/** 服务器端保存上载文件的文件名前缀 */
	protected String fileNamePrefix = "";

	/** 服务器端保存上载文件的相对 URL 路径 */
	protected String rootURLPath = "";
public FileProcessor(String uploadPropertiesFile) throws Exception {
	// 根据 uploadPropertiesFile 文件中的文件上载参数设置相关属性
	java.io.InputStream ins = null;
	try {
		ins = ClassLoader.getSystemResourceAsStream(uploadPropertiesFile);
		if (ins == null) {
			new Exception("配置文件找不到!");
		}
		java.util.Properties property = new java.util.Properties();
		property.load(ins);
		this.setRootPath(property.getProperty("ROOT_PATH"));
		this.setRootURLPath(property.getProperty("ROOT_URL_PATH"));
	} catch (IOException e) {
		e.printStackTrace();
		throw new Exception("读取配置文件文件上载参数错误");
	} finally {
		if (ins != null)
			ins.close();
	}
}
public FileProcessor(String rootPath,String relativePath,String rootURLPath,boolean holdOrgFileType,String fileType,String fileNamePrefix) {
	super();
	this.setRootPath(rootPath);
	this.setRootURLPath(rootURLPath);
	this.setRelativePath(relativePath);

	this.holdOrgFileType = holdOrgFileType;
	this.fileType  = fileType;
	this.fileNamePrefix = fileNamePrefix;
	
	//如果根路径为空,则采用默认路径
	if ((rootPath == null)||(rootPath.trim().equals(""))){
		if (File.separator.equals("\\")) rootPath ="c:\\www\\html";
		else rootPath = "/usr/lpp/internet/server_root/pub/zh_CN";
	}
	java.io.File f = new File("aa");
}
public boolean deleteUploadFiles() {
	int count = uploadFileNames.size();
	String filename;
	boolean b  = true;
	for(int i=0;i<count;i++){
		filename =(String) uploadFileNames.elementAt(i);
		java.io.File f = new java.io.File(filename);
		b = b && f.delete();
	}
	return b;
}
/*************************************************************************
 * 功能/function:	根据给定的 URL 文件名删除其在服务器上的物理文件
 * 参数/parameter:	filename:给定的 URL 文件名
 * 返回/return:		N/A
 * 步骤/steps :		
 * 修改记录/Revision
 * 		日期			修改人			描述
 *************************************************************************/
 
public boolean fileErase(String filename) {
	String s = getFileSavePath() + getRealFileName(filename);
	
	java.io.File f = new java.io.File(s);
	if (f.isFile()) return f.delete();
	else return true;
}
public String getFileNamePrefix() {
	return fileNamePrefix;
}
public String getFileSavePath() {
	return  rootPath + this.relativeFilePath;
}
public String getFileType() {
	return fileType;
}
public String getFileURLPath() {
	return  rootURLPath + this.relativeURLPath;
}
/************************************************************************************
 * 功能/function:	该函数读取所有参数名
 * 参数/parameter:	N/A
 * 返回/return:		所有参数名的一个String数组
 * 步骤/steps :		
 * 修改记录/Revision
 ************************************************************************************/
public String[] getParameterNames() {
	int i;
	int listSize = parameterNames.size();
	String[]  parameterNameList = new String[listSize];
	
	for(i=0;i<listSize;i++)
		parameterNameList[i] = (String)parameterNames.elementAt(i);

	return parameterNameList;
}
/************************************************************************************
 * 功能/function:	该函数通过指定的参数名读取参数值
 * 参数/parameter:	parameterName 参数名
 * 返回/return:		参数值,如果没有该参数返回null
 * 步骤/steps :		
 * 修改记录/Revision
 ************************************************************************************/
public String getParameterValue(String parameterName) {
	int    parameterIndex;
	String parameterValue;

	parameterIndex = parameterNames.indexOf(parameterName);
	if (parameterIndex < 0) {
		parameterValue = null;
	} else {
		parameterValue = (String)parameterValues.elementAt(parameterIndex);
	}

	if( parameterValue == null ) return "";
	else	return parameterValue;
}
/************************************************************************************
 * 功能/function:	该函数通过指定的参数名读取参数值
 * 参数/parameter:	parameterName 参数名
 * 返回/return:		参数值,如果没有该参数返回null
 * 步骤/steps :		
 * 修改记录/Revision
 ************************************************************************************/
public String getParameterValue(String parameterName,String defaultValue) {
	int    parameterIndex;
	String parameterValue;

	parameterIndex = parameterNames.indexOf(parameterName);
	if (parameterIndex < 0) {
		parameterValue = null;
	} else {
		parameterValue = (String)parameterValues.elementAt(parameterIndex);
	}

	if( parameterValue == null ) return defaultValue;
	else	return parameterValue;
}
/*****************************************************************
 * 功能/function:	返回文件路径字符串中的文件名
 * 参数/parameter:	file:包含路径信息的文件名字符串
 * 返回/return:		不包含路径信息的文件名字符串
 * 步骤/steps :		
 * 修改记录/Revision
 ******************************************************************/
 
public static String getRealFileName(String file) {
	if (file==null ) return "";
	file = file.trim();
	int j = file.lastIndexOf("/");
	int k = file.lastIndexOf("\\");
	int l =-1;
	if (j>l) l=j;
	if (k>l) l=k;
	return file.substring(l+1);
}
public String getRelativeFilePath() {
	return relativeFilePath;
}
public String getRelativeURLPath() {
	return relativeURLPath;
}
public String getRootPath() {
	return rootPath;
}
public String getRootURLPath() {
	return rootURLPath;
}
/************************************************************************************
 * 功能/function:	该函数通过上载文件参数名,读取上载后文件名
 * 参数/parameter:	parameterName 参数名
 * 返回/return:		上载后文件名,如果没有该参数返回null
 * 步骤/steps :		
 * 修改记录/Revision
 ************************************************************************************/
public String getUploadFileName(String parameterName){
	int    parameterIndex;
	String FileName;

	parameterIndex = uploadParameterNames.indexOf(parameterName);
		  
	if (parameterIndex < 0){
		FileName = null;
	}else{
		FileName = (String)uploadFileNames.elementAt(parameterIndex);
	}

	if( FileName == null ) return "";
	else	return FileName;
}
/************************************************************************************
 * 功能/function:	该函数通过上载文件参数名,读取上载文件大小
 * 参数/parameter:	parameterName 参数名
 * 返回/return:		上载文件大小,如果没有该参数返回null
 * 步骤/steps :		
 * 修改记录/Revision
 ************************************************************************************/
public int getUploadFileSize(String parameterName){
	int    parameterIndex;
	int fileSize;

	parameterIndex = uploadParameterNames.indexOf(parameterName);
		  
	if (parameterIndex < 0){
		fileSize = -1;
	}else{
		fileSize = ((Integer)uploadFileSizes.elementAt(parameterIndex)).intValue();
	}

	return fileSize;
}
/************************************************************************************
 * 功能/function:	该函数通过上载文件参数名,读取上载文件类型
 * 参数/parameter:	parameterName 参数名
 * 返回/return:		上载文件类型,如果没有该参数或无类型返回null
 * 步骤/steps :		
 * 修改记录/Revision
 ************************************************************************************/
public String getUploadFileType(String parameterName){
	int    parameterIndex;
	String fileType;

	parameterIndex = uploadParameterNames.indexOf(parameterName);
		  
	if (parameterIndex < 0){
		fileType = "";
	}else{
		fileType = (String)uploadFileTypes.elementAt(parameterIndex);
	}

	return fileType;
}
/************************************************************************************
 * 功能/function:	该函数通过上载文件参数名,读取上载后文件名
 * 参数/parameter:	parameterName 参数名
 * 返回/return:		上载后文件名,如果没有该参数返回null
 * 步骤/steps :		
 * 修改记录/Revision
 ************************************************************************************/
public String getUploadFileURL(String parameterName){
	String s = this.getUploadFileName(parameterName);
	if (s == null) s = "";
	if (!s.trim().equals("")) s = this.getFileURLPath() + getRealFileName(s);
	return s;
}
/************************************************************************************
 * 功能/function:	该函数读取上载文件所有参数名
 * 参数/parameter:	N/A
 * 返回/return:		所有参数名的一个String数组
 * 步骤/steps :		
 * 修改记录/Revision
 ************************************************************************************/
public String[] getUploadParameterNames() {
	int i;
	int listSize = uploadParameterNames.size();
	String[]  uploadParameterNameList = new String[listSize];
	
	for(i=0;i<listSize;i++)
		uploadParameterNameList[i] = (String)uploadParameterNames.elementAt(i);

	return uploadParameterNameList;
}
/************************************************************************************
 * 功能/function:	该函数通过上载文件参数名,读取上载文件原名
 * 参数/parameter:	parameterName 参数名
 * 返回/return:		上载文件原名,如果没有该参数返回null
 * 步骤/steps :		
 * 修改记录/Revision
 ************************************************************************************/
public String getUploadSrcFileName(String parameterName){
	int    parameterIndex;
	String srcFileName;

	parameterIndex = uploadParameterNames.indexOf(parameterName);
		  
	if (parameterIndex < 0){
		srcFileName = null;
	}else{
		srcFileName = (String)uploadSrcFileNames.elementAt(parameterIndex);
	}

	if( srcFileName == null ) return "";
	else	return srcFileName;
}
public boolean isHoldOrgFileType() {
	return holdOrgFileType;
}
/************************************************************************************
 * 功能/function:	该函数从Http请求的输入流中读取数据,按传输规则分解数据,
 *					并将附件放入指定的 rootPath + relativePath 目录
 * 参数/parameter:	req : Http请求类 ; tmpdir : 上载文件存放目录

⌨️ 快捷键说明

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