AppFileUtil.java
4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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 + "/";
}
}