`
zp19827
  • 浏览: 14865 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

file operate

    博客分类:
  • Code
 
阅读更多
	
/**
	 * 获取随机名称(文件)
	 * 
	 * @param fileRealName
	 * @return Created:2010-7-30
	 * @author:WKF28474
	 */
	public static String getRandomName(String fileRealName) {
		String temp_time = new SimpleDateFormat("yyyyMMddHHmmss")
				.format(new Date());
		String randomName = null;
		String end_point = "";
		int point_int = fileRealName.lastIndexOf(".");
		if (point_int != -1) {
			end_point = fileRealName.substring(point_int + 1,
					fileRealName.length()).toLowerCase();
			randomName = temp_time + "." + end_point;
		} else {
			randomName = temp_time;
		}
		return randomName;
	}
	
	/**
	 * 上传附件
	 * 
	 * @param path_name  需要将文件上传到什么目录
	 * @param attachment 需要上传的文件
	 * @param maxSize    文件的最大长度是多少(bite) 如果为-1表示没有限制
	 * @return
	 * @throws ApplicationException
	 * Created:2010-7-6
	 * @author:WKF28474
	 */
	public static String saveFile(String path_name, FormFile attachment,
			int maxSize) throws Exception {
		try {
			if (attachment == null) {
				throw new Exception("file is null"); // /file is null
			}
			// 验证是否内容为空
			int attachment_size = attachment.getFileSize();
			if (attachment_size == 0) {
				throw new Exception("file's size is 0"); // file's size is 0
			}
			// 验证文件大小,如果maxSise=-1表示没有限制
			if (maxSize != -1 && attachment_size > maxSize) {
				throw new Exception("file is large"); // 文件大小限制
			}
			
			// 生成在服务器上的文件名(上传到服务器后的文件名是什么)
			String attachment_name = attachment.getFileName();
			// 获取文件名
			String file_name = Test.getRandomName(attachment_name);
			// 上传附件
			uploadFile(attachment.getInputStream(), path_name, file_name);

			// 返回生成的文件名即可
			return file_name;
		} catch (Exception e) {
			throw new Exception("file upload faild", e);
		}
	}

	
	/**
	 * 用流的方式上传文件 Notes:
	 * 
	 * @param attachment
	 * @param path_name
	 * @param file_name
	 * @throws ApplicationException
	 *             Created:2010-7-6
	 * @author:WKF28474
	 */
	public static void uploadFile(InputStream is, String path_name,
			String file_name) throws Exception {
		BufferedOutputStream outB = null;
		BufferedInputStream inB = null;
		// 生成目录和文件
		File file = doBefore(path_name, file_name);
		try {
			outB = new BufferedOutputStream(new FileOutputStream(file));
			inB = new BufferedInputStream(is);
			// 循环上传数据
			int count = 0;
			int size = 1024;
			byte[] b = new byte[size];
			count = inB.read(b, 0, size);
			if (count > 0) {
				outB.write(b, 0, count);
			}
			while ((count = inB.read(b, 0, size)) > 0) {
				outB.write(b, 0, count);
			}
		} catch (Exception e) {
			throw new Exception("", e);
		} finally {
			try {
				outB.close();
				inB.close();
			} catch (Exception e) {
				throw new Exception("", e);
			}
		}
	}

	/**
	 * 创建文件 Notes:
	 * 
	 * @param absolutePath
	 * @param sFileName
	 * @return
	 * @throws ApplicationException
	 *             Created:2010-7-6
	 * @author:WKF28474
	 * @throws Exception
	 */
	public static File doBefore(String path_name, String file_name)
			throws Exception {
		File file = null;
		if (!new File(path_name).isDirectory()) {
			if (!new File(path_name).mkdirs()) {
				throw new Exception("");
			}
		}
		// 创建目标文件
		try {
			String fullFileName = Test.replaceWithAny(Test.replaceWithAny(
					path_name, "\\", "/")
					+ "/" + file_name, "//", "/");
			file = new File(fullFileName);
			if (!file.exists()) {
				file.createNewFile();
			} else {
				throw new Exception("");
			}
		} catch (Exception e) {
			throw new Exception("", e);
		}
		return file;
	}
	
	/**
	 * 删除文件和子文件
	 * 
	 * @param dirFile
	 * @throws Exception
	 *             Created:2010-8-19
	 * @author:WKF28474
	 */
	public static void forceDelDir(File dirFile) throws Exception {
		if (dirFile.exists()) {
			try {
				File childFile[] = dirFile.listFiles();
				for (int i = 0; i < childFile.length; i++) {
					if (childFile[i].isDirectory()) {
						forceDelDir(childFile[i]);
					} else {
						childFile[i].delete();
					}
				}
				// 删除目录
				dirFile.delete();
			} catch (Exception ex) {
				ex.printStackTrace();
				throw ex;
			}
		}
	}

	/**
	 * 读取指定目录下的Properties配置文件.
	 * 
	 * @param resource
	 * @return Created:2011-1-5
	 * @author:zKF29923
	 */
	public static Properties getProperties(String resource) {
		Properties properties = new Properties();
		try {
			properties.load(getStream(resource));
		} catch (IOException e) {
			throw new RuntimeException("couldn't load properties file '"
					+ resource + "'", e);
		}
		return properties;
	}
	
	/**
	 * 读取指定目录下文件的输入流.
	 * 
	 * @param resource
	 * @return
	 * Created:2011-1-5
	 * @author:zKF29923
	 */
	public static InputStream getStream(String resource) {
		InputStream inputStream = Test.class.getClassLoader()
				.getResourceAsStream(resource);

		return inputStream;
	}

    public static String replaceWithAny(String sourceStr, String sFlag, String sReplace)
    throws Exception {
		StringBuffer sb = new StringBuffer();
		sourceStr = sourceStr.trim();
		for (int index = sourceStr.indexOf(sFlag); index != -1; index = sourceStr
				.indexOf(sFlag)) {
			sb.append(sourceStr.substring(0, index)).append(sReplace);
			sourceStr = sourceStr.substring(index + sFlag.length()).trim();
		}

		sb.append(sourceStr);
		return sb.toString();
	}
分享到:
评论

相关推荐

    operate-file.rar_File Operate

    "operate-file.rar_File Operate"这个主题涵盖了文件操作的基本概念和常见功能,包括打开、复制、移动和修改文件。以下是对这些知识点的详细阐述: 1. **打开文件**:在计算机编程中,打开文件通常涉及使用特定的...

    DELPHI_file.rar_delphi file operate_delphi 文件操作

    在Delphi编程环境中,文件操作是一项基础且至关重要的任务,涉及到读取、写入、创建、删除、移动等文件及目录的操作。以下将详细介绍在Delphi中如何进行这些操作,并结合"DELPHI实现文件目录操作"这个主题展开讨论。...

    csv.rar_C csv_C# csv_File Operate csv_c++ csv_读取文件

    `File.OpenText`方法用于打开文件,然后通过`TextFieldParser`类可以设置分隔符,并逐行读取数据,将数据转换为强类型对象,非常适合在Windows Forms或WPF应用中使用clistctrl控件展示。 C++中,虽然没有内建的CSV...

    文本操作方法OpenFile and operateFile

    在C#编程中,"文本操作方法OpenFile and operateFile"是两个关键概念,它们是进行文件处理的基础。本文将详细讲解如何使用C#来打开文件(OpenFile)以及对文件进行操作(operateFile),这对于任何希望深入学习C#...

    file-Operate.zip_FileOperate

    本压缩包"file-Operate.zip_FileOperate"提供了一个关于文件流操作的实例,帮助我们更好地理解和应用这个概念。 文件流在C++、Java、Python等许多编程语言中都有广泛的应用。它基于输入/输出流(I/O Stream)的概念...

    file-operate.rar_visual c

    "file-operate.rar_visual c" 提供的是一款基于Visual C++实现的文件管理系统,它允许用户执行基本的文件管理任务,如删除、复制和重命名文件。下面将详细讨论相关知识点。 1. **文件系统**:文件系统是操作系统...

    file_operate.rar_文件_文件操作

    本资源"file_operate.rar"显然是一个关于文件操作的集合,包含了源代码示例和可能的文档资料,帮助开发者深入理解并实践文件操作的相关知识。 首先,我们来看"file.cpp"这个文件,它很可能是一个C++语言编写的源...

    基于java file 文件操作operate file of java的应用

    通过构造函数`File(String path)`可以创建一个`File`对象,如示例中的`new File("c:/hongten")`。`File`对象提供了多种方法来处理文件和目录: 1. **创建文件夹**: 使用`mkdir()`或`mkdirs()`方法可以创建单层或...

    File_Operate.rar_c file 函数_cfileoperate_c操作txt文件

    `c file`函数是C标准库提供的接口,允许程序员读写磁盘上的文件。本主题将深入探讨如何使用这些函数,特别是`fopen`, `fprintf`, `fscanf`, `fgets`, `fputs`, `fclose`等,来处理和操作`.txt`文本文件。`...

    operate_xlsx.zip

    在本文中,我们将深入探讨如何使用Golang语言处理Excel(xlsx)文件,主要基于提供的"operate_xlsx.zip"资源。这个压缩包包含了示例代码,用于演示如何在Go语言环境中读取、创建、写入以及保存xlsx文件。遵循以下...

    CFile_Operate.rar_cfile

    CFile file("example.txt", CFile::modeRead); ``` 这里`"example.txt"`是文件名,`CFile::modeRead`是打开模式,表示以只读方式打开。 ### 3. 文件打开与关闭 使用`CFile::Open`或构造函数打开文件后,可以通过`...

    operateXml:操作xml文件

    File file = new File("path_to_xml_file.xml"); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = ...

    filemanager-plugin:在构建之前和之后复制,压缩解压缩(.zip.tar.gz.tgz),移动,删除文件或目录

    npm install filemanager-plugin --save-dev 用法 大事记 在构建过程中控制运行顺序。 start {Object} :将操作注册到挂钩,以编译器。 创建编译参数后执行插件。 end {Object} :注册动作以对编译器的挂接,编译...

    文件读写操作类

    如#ifndef FILE_OPERATE_H_IF #define FILE_OPERATE_H_IF #include using namespace std; class Fe_Sd_Error{/*****给数据模型录入数据 数据溢出*****/}; class Ee_er_error{/**执行插入文件 文件打开失败**/}; ...

    hdfs_design.rar_HDFS-OPERATE_hadoop_hadoop java_hdfs

    首先,HDFS的设计理念基于Google的GFS(Google File System)论文,旨在处理海量数据,提供高吞吐量的数据访问。它将大文件分割成多个块,并将这些块分布在多台机器上,确保即使部分节点故障,数据仍然可以被访问。...

    dbf.rar_DBF_dbase_open_open dbf file

    在描述中,“open Dbase file dbf and operate dbf data”明确了我们主要讨论的是如何打开DBF文件以及如何对其中的数据进行操作。DBF文件通常包含列名、数据类型和实际数据行,它们是结构化的表格数据。 标签"dbf...

    Java-API-Operate-Hadoop.rar_hadoop_hadoop api

    它的核心组件包括HDFS(Hadoop Distributed File System)和MapReduce,而Java API是开发者与Hadoop交互的主要方式。本文将深入探讨Java如何操作Hadoop,以及在"Java-API-Operate-Hadoop.rar"压缩包中提供的资源。 ...

    java对文件的复制和删除(文件夹、文件)

    在这个场景中,我们看到一个名为"OperateFile"的压缩包文件,它很可能包含了一个Java类或者一个项目,用于演示如何使用Java来执行这些操作。下面将详细讨论Java中文件复制和删除的关键知识点,以及可能用到的相关API...

    dbunit-masterPHPUnit测试库.zip

     * An interface for classes that require a list of databases to operate.  */ interface DatabaseListConsumer {  /**  * Sets the database for the spec  *  * @param ...

    Quartus II 调试Error和Warning及解决办法

    9. warning: circuit may not operate.detected 46 non-operational paths clocked by clock clk44 with clock skew larger than data delay 这个警告是由于时钟抖动大于数据延时,当时钟很快,而 if 等类的层次...

Global site tag (gtag.js) - Google Analytics