日期:2014-05-19 浏览次数:20899 次
方法1:写一个SERVLET
?
public class ShowPictureServlet extends HttpMsgConnector { private static final long serialVersionUID = 1L; private final static Logger logger = Logger.getLogger(ShowPictureServlet.class.getSimpleName()); @Override public void execute(HttpServletRequest req, HttpServletResponse res) throws Exception { String imagePath = req.getParameter(FSConstant.PARAM_PATH); String module = req.getParameter(FSConstant.PARAM_MODULE); logger.debug("----------Getting image path--->" + imagePath); logger.debug("----------Getting module name--->" + module); OutputStream os = res.getOutputStream(); if(StringUtils.isNotEmpty(imagePath) && StringUtils.isNotEmpty(module)) { imagePath = getPrePath(Modules.valueOf(module)) + imagePath; imagePath = imagePath.replace("/", File.separator).replace("\\", File.separator); logger.debug("----------Image real path is--->" + imagePath); InputStream imageIn = new FileInputStream(new File(imagePath)); if(imagePath.toLowerCase().endsWith(".png")) { res.setContentType("image/png;charset=utf-8"); } else if(imagePath.toLowerCase().endsWith(".jpg")){ res.setContentType("image/jpeg;charset=utf-8"); } else { res.setContentType("image/gif;charset=utf-8"); } BufferedInputStream bis = new BufferedInputStream(imageIn); BufferedOutputStream bos = new BufferedOutputStream(os); byte data[] = new byte[4096]; int size = 0; size = bis.read(data); while(size != -1) { bos.write(data, 0, size); size = bis.read(data); } bis.close(); bos.flush(); bos.close(); } else { logger.warn("----------No parameter of image path or module name received from mobile client--->"); } os.flush(); os.close(); } private String getPrePath(Modules moduleType) { switch (moduleType) { case SERVICE: return FSConstant.SERVICE_PATH; case REWARDS: return FSConstant.REWARDS_PATH; case PROMOTION: return FSConstant.PROMOTION_PATH; case RESTAURANT: return FSConstant.RESTAURANT_IMAGE_PATH; case COMMENTARY: return FSConstant.RESTAURANT_COMMENTARY_PATH; default: logger.error("----------Error module name--->" + moduleType.name()); return null; } } }
?简单解释一下,参数imagePath是存放在数据库中的图片相对路径,module是模块名,根据这两个参数可以获取当前图片的真实路径。
?
下面是在web.xml里的配置:
?
<servlet> <servlet-name>ShowImageServlet</servlet-name> <servlet-class>com.molisc.mobileserver.core.connection.ShowPictureServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ShowImageServlet</servlet-name> <url-pattern>/ShowImage.action</url-pattern> </servlet-mapping>
?
使用方法参考如下:
?
http://localhost:8080/testapp/ShowImage.action?model=SERVICE&path=/20110403/a.png?
?
方法二:在TOMCAT/JBOSS中添加一个Context
?
<Context path="/picContext" docBase="/home/leozhou/apps/test"/>
?其中/home/leozhou/apps/test是我的图片存放路径。
?
使用方法参考如下:
?
http://localhost:8080/picContext/20110403/a.png?
?
?