Commit 1060bdd0 authored by 陈精华's avatar 陈精华 Committed by kl

新功能点:支持base url配置(主要用于nginx反向代理等)

parent 8c2fb2bd
......@@ -31,6 +31,9 @@ spring.http.multipart.max-file-size=100MB
cache.clean = true
#######################################可在运行时动态配置#######################################
#提供预览服务的地址,默认从请求url读,如果使用nginx等反向代理,需要手动设置
#base.url = https://file.keking.cn
#是否启用缓存
cache.enabled = true
......
......@@ -23,6 +23,7 @@ public class ConfigConstants {
private static String ftpPassword;
private static String ftpControlEncoding;
private static String fileDir = OfficeUtils.getHomePath() + File.separator + "file" + File.separator;
private static String baseUrl;
public static Boolean isCacheEnabled() {
return cacheEnabled;
......@@ -92,6 +93,15 @@ public class ConfigConstants {
return fileDir;
}
public static String getBaseUrl() {
return baseUrl;
}
public static void setBaseUrl(String baseUrl) {
// 不以'/'结尾的,加上'/'
ConfigConstants.baseUrl = baseUrl.concat("/");
}
@Value("${file.dir:default}")
public void setFileDir(String fileDir) {
if (!"default".equals(fileDir)) {
......
......@@ -5,6 +5,7 @@ import org.artofsolving.jodconverter.office.OfficeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import javax.annotation.PostConstruct;
import java.io.BufferedReader;
......@@ -29,6 +30,7 @@ public class ConfigRefreshComponent {
public static final String DEFAULT_FTP_USERNAME = null;
public static final String DEFAULT_FTP_PASSWORD = null;
public static final String DEFAULT_FTP_CONTROL_ENCODING = "UTF-8";
public static final String BASE_URL = null;
@PostConstruct
......@@ -53,6 +55,7 @@ public class ConfigRefreshComponent {
String ftpPassword;
String ftpControlEncoding;
String configFilePath = OfficeUtils.getCustomizedConfigPath();
String baseUlr;
while (true) {
FileReader fileReader = new FileReader(configFilePath);
BufferedReader bufferedReader = new BufferedReader(fileReader);
......@@ -67,6 +70,7 @@ public class ConfigRefreshComponent {
ftpControlEncoding = properties.getProperty("ftp.control.encoding", DEFAULT_FTP_CONTROL_ENCODING);
textArray = text.split(",");
mediaArray = media.split(",");
baseUlr = properties.getProperty("base.url", null);
ConfigConstants.setCacheEnabled(cacheEnabled);
ConfigConstants.setSimText(textArray);
ConfigConstants.setMedia(mediaArray);
......@@ -75,6 +79,9 @@ public class ConfigRefreshComponent {
ConfigConstants.setFtpUsername(ftpUsername);
ConfigConstants.setFtpPassword(ftpPassword);
ConfigConstants.setFtpControlEncoding(ftpControlEncoding);
if (baseUlr != null && !StringUtils.isEmpty(baseUlr)) {
ConfigConstants.setBaseUrl(baseUlr);
}
bufferedReader.close();
fileReader.close();
Thread.sleep(1000L);
......
package cn.keking.filters;
import cn.keking.config.ConfigConstants;
import org.springframework.util.StringUtils;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
......@@ -19,10 +22,16 @@ public class ChinesePathFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
StringBuilder pathBuilder = new StringBuilder();
pathBuilder.append(request.getScheme()).append("://").append(request.getServerName()).append(":")
.append(request.getServerPort()).append(((HttpServletRequest)request).getContextPath()).append("/");
request.setAttribute("baseUrl", pathBuilder.toString());
String baseUrl;
if (ConfigConstants.getBaseUrl() != null) {
baseUrl = ConfigConstants.getBaseUrl();
} else {
StringBuilder pathBuilder = new StringBuilder();
pathBuilder.append(request.getScheme()).append("://").append(request.getServerName()).append(":")
.append(request.getServerPort()).append(((HttpServletRequest) request).getContextPath()).append("/");
baseUrl = pathBuilder.toString();
}
request.setAttribute("baseUrl", baseUrl);
chain.doFilter(request, response);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment