兼容Windows 和 Linux 的文件读写工具类

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 文件操作
 * @author 
 */
public class FileUploadConstants {

	/** 文件上传根目录* */
	public static String UPLOAD_FILE_ROOT = "";
	/** 教材封面上传路径 **/
	public static String HEAD_PATH = "";
	
	public static String LIVE_TEACHING = "";
	
	public static String LIVE_STUDENT = "";//学生上传头像
	public static String LIVE_TEACHER = "";//老师上传头像
	public static String PRIVETE_KEY_PATH = "";//TLS私钥路径
	public static String PUBLIC_KEY_PATH = "";//TLS公钥路径
	public static String FILEUPLOAD_IPIMGADRESS = "";//头像路径
	
	//判断机器是什么系统
	public static boolean isLinux() {
		String osType = System.getProperties().getProperty("os.name").toLowerCase();
		if (osType.startsWith("windows")) {
			return false;
		} else {
			return true;
		}
	}

	private static Properties prop = null;
	static {
		//window环境下文件路径
		String path = "/com/conf/system_windows.properties";
		if (isLinux()) {
			//Linux 环境下的文件路径
			path = "/com/conf/system_linux.properties";
		}
		//通过类加载器来读取文件
		InputStream in = FileUploadConstants.class.getResourceAsStream(path);
		
		if (in != null) {
			prop = new Properties();
			try {
				prop.load(in);
				UPLOAD_FILE_ROOT = (String) prop.get("upload_file_root");
				HEAD_PATH = (String) prop.get("head_path");
				LIVE_TEACHING = (String) prop.get("live_teaching");
				LIVE_STUDENT = (String) prop.get("live_student");
				LIVE_TEACHER = (String) prop.get("live_teacher");
				PRIVETE_KEY_PATH = (String) prop.get("private_key_path");
				PUBLIC_KEY_PATH = (String) prop.getProperty("public_key_path");
				FILEUPLOAD_IPIMGADRESS = (String) prop.getProperty("ipImgAdress");
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
	}

	//根据指定的Key来获取文件路径
	public static String getPropValue(String key) {
		String path = "/com/conf/system_windows.properties";
		String val = null;
		if (isLinux()) {
			path = "/com/conf/system_linux.properties";
		}
		InputStream in = FileUploadConstants.class.getResourceAsStream(path);
		if (in != null) {
			prop = new Properties();
			try {
				prop.load(in);
				val = prop.getProperty(key);
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println("=====================get property file error!");
			}
		}
		return val;
	}

	//创建目录
	public  static boolean mkdirs(String path) {
		boolean result = false;
		File file = new File(path);
		// 如果文件存在直接返回
		if (file.exists()) {
			result = true;
		}
		// 否则创建路径
		else {
			result = file.mkdirs();
		}
		return result;
	}
	
	
	//为了兼容Linux 和  Window将文件路径进行转换 
	public static String parseToPath(String str) {
		str = str.replace('\\', '/');
		str = str.replaceAll("/{2,}","/");
		return str;
	 }
	 
	//保存文件
	public static  void saveFile(InputStream inputStream, String fileName) throws IOException {
		FileOutputStream fos = new FileOutputStream(new File(fileName));

		byte[] buff = new byte[1024*4];
		int flag = 0;

		while ((flag = inputStream.read(buff, 0, buff.length)) != -1) {
			fos.write(buff, 0, flag);
		}

		fos.close();
	}
	
	 
	 public static void main(String[] args) throws Exception {
		
		 FileInputStream fis = new FileInputStream("d:/1.jpg");
		
		 File file = new File(UPLOAD_FILE_ROOT+HEAD_PATH);
		 file.mkdirs();
		 
		 saveFile(fis, "Z:\\live\\teaching\\course\\20150901\\212.jpg");
	}
}

猜你喜欢

转载自blog.csdn.net/a1106900429/article/details/48313297