图片预览–http访问服务器路径
解决:限于tomcat容器启动的项目,其它容器没试过,可能无效。
需求原因:正常访问图片,如果图片存储在工程的web路径下,是可以java中把路径给到src,直接访问。
但是,如果图片在服务器上,则tomcat访问不到tomcat以外的东西。需配置tomcat目录下cof–server.xml文件
<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true" xmlNamespaceAware="false" xmlValidation="false">
-----------------------------------------------------------------------------------------------------
<Context docBase="/tomcattest/document/" path="/file" reloadable="true" debug="0" crossContext="true"/>
----------- -----------------------------------------------------------------------------------
<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->
<!-- Access log processes all example.
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/>
-->
</Host>
上述代码,虚线-----------框起来的,为要修改的路径。
docBase="/tomcattest/document/" path="/file"
从启tomcat容器,file地址会映射为"/tomcattest/document/"
下面附上,这次预览的完整代码
一、前端页面
//预览
function search(id){
var gridData = $('#jqGrid').jqGrid('getRowData',id);
var openurl = "${appName}/t9c_employee_resumes/upload_papers/getSearchPic.ajax?id="+gridData.id;
popLayer.paramLayerPhase=top.FloatingLayers.create({ //top ===顶级窗口滑出div窗口
background: true,
width:'50%',
title: {
text: '预览',
icon: 'fi fi-password-lock'
},
src: openurl,
});
popLayer.paramLayerPhase.show();
}
二、业务层ServiceImpl
/**
* 预览
* @param modelMap
* @param t9c_employee_papersDTO
* @param request
* @return
*/
@RequestMapping(value="/getSearchPic.ajax")
public String getSearchPic(ModelMap modelMap,T9c_employee_papersDTO t9c_employee_papersDTO,HttpServletRequest request) {
T9c_employee_papersDTO papers=t9c_employee_papersService.getT9c_employee_papaersById(t9c_employee_papersDTO);
String path=papers.getAddress();
String osName = System.getProperty("os.name");
String endPath=new String();
if(osName.toLowerCase().indexOf("win")>=0){//windows操作系统
endPath=path.substring(22);
}else{
//endPath=path.substring(17);
endPath=path.substring(21);
}
//配置服务器路径,从localhost开始拼路径
String host = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()+"/file/"+endPath;
modelMap.put("host", host);
modelMap.put("papers", papers);
return "employee/t9c_employee_resumes/t9c_employee_search_pic";
}
希望可以帮助到现在需要的你。