import java.io.File;
import java.net.URL;
import javax.servlet.ServletException;
import org.apache.struts2.ServletActionContext;
/**
* 程序中的很多地方需要用到request.getRealPath()或者getServletContext.getRealPath()。这个方法受到war 和non-war的影响,以及不同app server实现的影响,返回的结果往往不一样,在weblogic中会返回null。
一般的应用都会有一个或几个配置文件放在web-inf下面,getRealPaht返回null是读配置文件有很大困难。
* @author 流浪鱼
*
*/
public class AbsolutePathUtils {
/**
* 通过上下文来取工程路径
*
* @return
* @throws Exception
*/
private String getAbsolutePathByContext() throws Exception {
String webPath = ServletActionContext.getServletContext().getRealPath("/");
webPath = webPath.replaceAll("[\\\\\\/]WEB-INF[\\\\\\/]classes[\\\\\\/]?", "/");
webPath = webPath.replaceAll("[\\\\\\/]+", "/");
webPath = webPath.replaceAll("%20", " ");
if (webPath.matches("^[a-zA-Z]:.*?$")) {
} else {
webPath = "/" + webPath;
}
webPath += "/";
webPath = webPath.replaceAll("[\\\\\\/]+", "/");
return webPath;
}
/**
* 通过类路径来取工程路径
*
* @return
* @throws Exception
*/
private String getAbsolutePathByClass() throws Exception {
String webPath = this.getClass().getResource("/").getPath().replaceAll("^\\/", "");
webPath = webPath.replaceAll("[\\\\\\/]WEB-INF[\\\\\\/]classes[\\\\\\/]?", "/");
webPath = webPath.replaceAll("[\\\\\\/]+", "/");
webPath = webPath.replaceAll("%20", " ");
if (webPath.matches("^[a-zA-Z]:.*?$")) {
} else {
webPath = "/" + webPath;
}
webPath += "/";
webPath = webPath.replaceAll("[\\\\\\/]+", "/");
return webPath;
}
private String getAbsolutePathByResource() throws Exception {
URL url = ServletActionContext.getServletContext().getResource("/");
String path = new File(url.toURI()).getAbsolutePath();
if (!path.endsWith("\\") && !path.endsWith("/")) {
path += File.separator;
}
return path;
}
public String getAbsolutePath() throws ServletException {
String webPath = null;
try {
webPath = getAbsolutePathByContext();
} catch (Exception e) {
}
// 在weblogic 11g 上可能无法从上下文取到工程物理路径,所以改为下面的
if (webPath == null) {
try {
webPath = getAbsolutePathByClass();
} catch (Exception e) {
}
}
if (webPath == null) {
try {
webPath = getAbsolutePathByResource();
} catch (Exception e) {
}
}
System.out.println(webPath);
return webPath;
}
}
使用如下:
String photoUrl = "images"+File.separator+"small.jpg";
String absolutePath = new AbsolutePathUtils().getAbsolutePath();
System.out.println("--------------"+absolutePath);
File file = new File(absolutePath+photoUrl);//Struts2Utils.getSession().getServletContext().getRealPath(File.separator)
FileInputStream fis = new FileInputStream(file);
int i = 0;
while ((i = fis.read(b)) != -1) {
os.write(b, 0, i);
}
os.close();
fis.close();
List<CityZyGrzp> list = this.find(sb.toString(), new HashMap<String, String>());
if(list.size()==1){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
CityZyGrzp result = (CityZyGrzp) (list.get(0));
try {
if(result.getZp()!=null) {
IOUtils.copy(result.getZp().getBinaryStream(), baos);
result.setZpStream(baos.toByteArray());
}
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(baos!=null){
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}else{
return null;
}