AppFileUtil.java 4.48 KB
package com.hotent.file.util;


import com.hotent.base.context.BaseContext;
import com.hotent.base.util.AppUtil;
import com.hotent.base.util.BeanUtils;
import com.hotent.base.util.StringUtil;
import com.hotent.file.model.UploadProperties;
import com.hotent.file.service.FlowUploadPropertiesService;
import com.hotent.file.service.SysPropertyService;

import javax.servlet.ServletContext;
import java.io.File;
import java.util.Calendar;

/**
 * 附件工具类
 * @company 广州宏天软件股份有限公司
 * @author:lj
 * @date:2018年6月15日
 */
public class AppFileUtil {
	/**
	 * 获取系统属性中的文件存储路径
	 * @return
	 */
	public static String getAttachPath() {
		SysPropertyService sysPropertyService = AppUtil.getBean(SysPropertyService.class);
		return sysPropertyService.getByAlias("file.upload", "D:\\x7\\file");
	}


	/**
	 * 配置文件中获取文件存放的类型
	 * @author xcx
	 * @version 创建时间:2013-12-27  下午3:53:20
	 * @return
	 */
	public static String getSaveType(String propertiesId) {
		if(StringUtil.isNotEmpty(propertiesId)){
			FlowUploadPropertiesService flowUploadPropertiesService = AppUtil.getBean(FlowUploadPropertiesService.class);
			UploadProperties uploadProperties = flowUploadPropertiesService.getUploadProperties(propertiesId);
			if(BeanUtils.isNotEmpty(uploadProperties) && BeanUtils.isNotEmpty(uploadProperties.getUploadType())){
				return uploadProperties.getUploadType();
			}
		}
		SysPropertyService sysPropertyService = AppUtil.getBean(SysPropertyService.class);
		return sysPropertyService.getByAlias("file.saveType", "database");
	}


	/**
	 * 创建文件目录
	 *            文件名称
	 * @return 文件的完整目录/year/tenant_id_/month/account
	 */
	public static String createFilePath(String tempPath, String fileName) {
		BaseContext baseContext = AppUtil.getBean(BaseContext.class);
		String tenantId = baseContext.getCurrentTenantId();
		Calendar cal = Calendar.getInstance();
		Integer year = cal.get(Calendar.YEAR); // 当前年份
		Integer month = cal.get(Calendar.MONTH) + 1; // 当前月份
		File one = new File(File.separator +  year + File.separator + tenantId + File.separator + month  + File.separator + tempPath +  File.separator);
		if (!one.exists()) {
			one.mkdirs();
		}
		return one.getPath() + File.separator + fileName;
	}

	/**
	 * 获取文件路径
	 * @param tempPath
	 * @param fileName
	 * @return
	 */
	public static String getFilePath(String tempPath, String fileName) {
		BaseContext baseContext = AppUtil.getBean(BaseContext.class);
		String tenantId = baseContext.getCurrentTenantId();
		Calendar cal = Calendar.getInstance();
		Integer year = cal.get(Calendar.YEAR); // 当前年份
		Integer month = cal.get(Calendar.MONTH) + 1; // 当前月份
		File one = new File(File.separator +  year + File.separator + tenantId + File.separator + month  + File.separator + tempPath +  File.separator);
		return one.getPath() + File.separator + fileName;
	}

	/**
	 * 配置文件中获取文件上传路径
	 * 如果为空则采用默认路径/attachFiles/temp
	 * 这个路径返回没有/或\结尾。
	 *
	 * @author hjx
	 * @version 创建时间:2013-11-4  下午3:46:28
	 * @return
	 */
	public static String getBasePath() {
		String attachPath=getAttachPath();
		if (StringUtil.isEmpty(attachPath)) {
			attachPath = AppFileUtil.getRealPath("/attachFiles/temp");
		}
		attachPath=StringUtil.trimSufffix(attachPath, "\\") ;
		attachPath=StringUtil.trimSufffix(attachPath, "/") ;

		return attachPath;
	}

	public static String createPath(String tempPath, String fileName) {
		File one = new File(tempPath);
		if (!one.exists()) {
			one.mkdirs();
		}
		return one.getPath() + File.separator + fileName;
	}

	private static ServletContext servletContext;


	/**
	 *
	 * @param _servletContext
	 */
	public static void init(ServletContext _servletContext)
	{
		servletContext=_servletContext;
	}

	/**
	 * 在web环境中根据web页面的路径获取对应页面的绝对路径。
	 * @param path
	 * @return
	 */
	public static String getRealPath(String path){
		return servletContext.getRealPath(path);
	}
	/**
	 * 获取阿里云OSS文件上传文件夹
	 * @param tempPath
	 * @return
	 */
	public static String getAliOssForderPath(String tempPath) {
		BaseContext baseContext = AppUtil.getBean(BaseContext.class);
		String tenantId = baseContext.getCurrentTenantId();
		Calendar cal = Calendar.getInstance();
		Integer year = cal.get(1);
		Integer month = cal.get(2) + 1;
		return year + "/" + tenantId + "/" + month + "/" + tempPath + "/";
	}
}