最近在spring boot项目静态类中获取resource路径下文件,在idea中启动都可以获取,但是打包后变成了jar包 就无法获取到。
被获取的文件如下:
package com.niubo.util.ipFilter;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
/**
* @Author Xiao HE
* @Create Time 2019/9/7 13:52
**/
@Component
public class DocUtil {
// 模板文件名称 位于 resource/iptest下面
private String[] ftlArray = {"ip2region.db"};
public String createFtlFileByFtlArray() {
String ftlPath = "iptest/";
String path = "";
for (int i = 0; i < ftlArray.length; i++) {
path = createFtlFile(ftlPath, ftlArray[i]);
if (null == path) {
}
}
return path;
}
private String createFtlFile(String ftlPath, String ftlName) {
try {
//获取当前项目所在的绝对路径
String proFilePath = System.getProperty("user.dir");
//获取模板下的路径
String newFilePath = proFilePath + File.separator + "src" + File.separator + "main" + File.separator + "resources" + File.separator + ftlPath;
newFilePath = newFilePath.replace("/", File.separator);
//检查项目运行时的src下的对应路径
File newFile = new File(newFilePath + ftlName);
if (newFile.isFile() && newFile.exists()) {
return newFilePath;
}
//当项目打成jar包会运行下面的代码,并且复制一份到src路径下(具体结构看下面图片)
InputStream certStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(ftlPath + ftlName);
byte[] certData = org.apache.commons.io.IOUtils.toByteArray(certStream);
FileUtils.writeByteArrayToFile(newFile, certData);
return newFilePath;
} catch (Exception e) {
}
return null;
}
}
package com.niubo.util.ipFilter;
import javax.servlet.http.HttpServletRequest;
/**
* @author Drew
* @title: Ipsettings
* @projectName ybadmin
* @description: TODO
* @date 2019/5/31 13:37
*/
public class Ipsettings {
//获取用户IP
public static String getRemoteHost(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
}
package com.niubo.util.ipFilter;
import java.io.File;
import java.lang.reflect.Method;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.lionsoul.ip2region.DataBlock;
import org.lionsoul.ip2region.DbConfig;
import org.lionsoul.ip2region.DbSearcher;
import org.lionsoul.ip2region.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* @Author Xiao HE
* @Create Time 2019/9/7 9:51
**/
@Component
public class IPUtil {
@Autowired
DocUtil docUtil;
public String getCityInfo(String ip){
//db
String dbPath = docUtil.createFtlFileByFtlArray()+"ip2region.db";
System.out.println(dbPath);
File file = new File(dbPath);
if ( file.exists() == false ) {
System.out.println("Error: Invalid ip2region.db file");
}
//查询算法
int algorithm = DbSearcher.BTREE_ALGORITHM; //B-tree
//DbSearcher.BINARY_ALGORITHM //Binary
//DbSearcher.MEMORY_ALGORITYM //Memory
try {
DbConfig config = new DbConfig();
DbSearcher searcher = new DbSearcher(config, dbPath);
//define the method
Method method = null;
switch ( algorithm )
{
case DbSearcher.BTREE_ALGORITHM:
method = searcher.getClass().getMethod("btreeSearch", String.class);
break;
case DbSearcher.BINARY_ALGORITHM:
method = searcher.getClass().getMethod("binarySearch", String.class);
break;
case DbSearcher.MEMORY_ALGORITYM:
method = searcher.getClass().getMethod("memorySearch", String.class);
break;
}
DataBlock dataBlock = null;
if ( Util.isIpAddress(ip) == false ) {
System.out.println("Error: Invalid ip address");
}
dataBlock = (DataBlock) method.invoke(searcher, ip);
return dataBlock.getRegion();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
@Autowired
IPUtil ipUtil;
@RequestMapping(value = "/loginTest", method = RequestMethod.POST)
public ResponseObj getIpTest(HttpServletRequest request){
String remoteHost = getRemoteHost(request);
String cityInfo = ipUtil.getCityInfo(remoteHost);
return ResponseObj.createSuccessResponse(cityInfo);
}