获取服务器的地址

 

 

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;
		}

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值