将文件写入到本地磁盘-IO流使用

       /**
	 * 将文件写入到本地磁盘
	 * @param dirPath	目录路径
	 * @param fileName	文件名,包含文件后缀
	 * @param buffer 	文件
	 * @return	文件路径
	 */
	public String writeToFile(String dirPath, String fileName, String buffer) {
		String NCHome = RuntimeEnv.getInstance().getProperty(RuntimeEnv.SERVER_LOCATION_PROPERTY);
		dirPath = NCHome + File.separator + "upload";
		File file = new File(dirPath);
		if (!file.exists()) {
			file.mkdirs();
		}
		SimpleDateFormat df = new SimpleDateFormat("_yyyy_MM_dd_HHmmss");
		fileName = fileName + df.format(new Date());
		String filePath = dirPath + File.separator + fileName;
		try {
			OutputStream out = new FileOutputStream(filePath);
			Writer writer = new java.io.OutputStreamWriter(out, "UTF-8");//UTF-8,GB2312
			writer.write(buffer.toString());
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return filePath;
	}
posted @ 2021-06-16 13:37  亚州Asu  阅读(763)  评论(0编辑  收藏  举报