通过配置action的Stream各项参数,虽然可以进行文件下载,但是却需要把这些信息硬编码在action中,我们在编写代码时,并不知道用户将会访问的资源名,怎么办?实现方法应该有很多很多种了..看代码吧. 不废话了.
Struts2.xml文件内容
<struts>
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<!-- 配了两个action,第一个用来显示所有商品,第二个用来动态显示商品图片 -->
<action name="showProducts" class="com.download.action.ShowProducts">
<result>/jsp/product.jsp</result>
</action>
<action name="getImage" class="com.download.action.GetImage">
<result name="success" type="stream">
<param name="inputName">inputStream</param>
</result>
</action>
</package>
</struts>
商品类,Product.java内容
package com.download.domain;
public class Product {
private int id;
private String name;
public Product(){}
public Product(int id,String name){
this.id=id;
this.name=name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
显示所有商品列表,ShowProducts.java内容
package com.download.action;
import java.util.ArrayList;
import java.util.List;
import com.download.domain.Product;
import com.opensymphony.xwork2.ActionSupport;
public class ShowProducts extends ActionSupport {
public List<Product> getProducts(){
List<Product> list=new ArrayList<Product>();
list.add(new Product(1,"电视"));
list.add(new Product(2,"电脑"));
list.add(new Product(3,"影碟机"));
list.add(new Product(4,"游戏机"));
return list;
}
}
动态获得商品图片,GetImage.java内容
package com.download.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.dispatcher.StreamResult;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Result;
public class GetImage extends ActionSupport implements ServletContextAware, ServletResponseAware{
private int productId;
public void setProductId(int productId) {
this.productId = productId;
}
private HttpServletResponse httpServletResponse;
private ServletContext servletContext;
@Override
public void setServletResponse(HttpServletResponse httpServletResponse) {
this.httpServletResponse=httpServletResponse;
}
@Override
public void setServletContext(ServletContext servletContext) {
this.servletContext=servletContext;
}
public InputStream getInputStream() throws Exception{
//文件类型
String contentType="image/gif";
//路径
String imageDirectory=servletContext.getRealPath("iamges");
File file=new File(imageDirectory,productId+".gif");
if(!file.exists()){
file=new File(imageDirectory,productId+".jpg");
contentType="image/jpeg";
}
if(!file.exists()){
file=new File(imageDirectory,"noimage.jpg");
contentType="image/jpeg";
}
if(file.exists()){
Result result=ActionContext.getContext().getActionInvocation().getResult();
if(result!=null && result instanceof InputStream){
StreamResult streamResult=(StreamResult)result;
streamResult.setContentType(contentType);
}
return new FileInputStream(file);
}
return null;
}
}
商品显示页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Download2</title>
</head>
<body>
<div>
<h1>商品列表</h1>
<table>
<tr>
<td>Name</td>
<td>Pricture</td>
</tr>
<s:iterator value="products" id="product">
<tr>
<td><s:property value="#product.name"/></td>
<td>
<s:url action="getImage" id="url">
<s:param name="productId">
<s:property value="#product.id"/>
</s:param>
</s:url>
<img src="<s:property value="#url"/>" width="100" height="50"/>
</td>
</tr>
</s:iterator>
</table>
<s:debug/>
</div>
</body>
</html>
访问如下:
注意:
启动tomcat的时候,遇到一点问题.看错误信息,检查了半天..结果还是这样,网上找了下.说没有commons-fileupload.jar造成的,但是我有这个jar包啊。。后来把jar包全部删掉,重新在Struts的示例文件里copy了一份,就正常了.错误信息如下:
Unable to load configuration. - action - file:/D:/1Y2/Java%E6%96%B9%E5%90%91/Struts2_LL/.metadata/.me_tcat/webapps/download/WEB-INF/classes/struts.xml:18:63
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:58)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:374)
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:418)
at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:69)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:51)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:275)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:397)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:108)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3693)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4340)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:920)
at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:883)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:492)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1138)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:516)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:566)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Caused by: Action class [com.download.action.DownLoad] not found - action - file:/D:/1Y2/Java%E6%96%B9%E5%90%91/Struts2_LL/.metadata/.me_tcat/webapps/download/WEB-INF/classes/struts.xml:18:63
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.verifyAction(XmlConfigurationProvider.java:409)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addAction(XmlConfigurationProvider.java:354)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addPackage(XmlConfigurationProvider.java:468)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadPackages(XmlConfigurationProvider.java:264)
at org.apache.struts2.config.StrutsXmlConfigurationProvider.loadPackages(StrutsXmlConfigurationProvider.java:111)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:193)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:55)
... 31 more
2010-9-4 22:50:35 org.apache.catalina.core.StandardContext filterStart
严重: Exception starting filter struts2
Unable to load configuration. - action - file:/D:/1Y2/Java%E6%96%B9%E5%90%91/Struts2_LL/.metadata/.me_tcat/webapps/download/WEB-INF/classes/struts.xml:18:63
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:431)
at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:69)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:51)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:275)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:397)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:108)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3693)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4340)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:920)
at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:883)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:492)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1138)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:516)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:566)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Caused by: Unable to load configuration. - action - file:/D:/1Y2/Java%E6%96%B9%E5%90%91/Struts2_LL/.metadata/.me_tcat/webapps/download/WEB-INF/classes/struts.xml:18:63
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:58)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:374)
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:418)
... 29 more
Caused by: Action class [com.download.action.DownLoad] not found - action - file:/D:/1Y2/Java%E6%96%B9%E5%90%91/Struts2_LL/.metadata/.me_tcat/webapps/download/WEB-INF/classes/struts.xml:18:63
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.verifyAction(XmlConfigurationProvider.java:409)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addAction(XmlConfigurationProvider.java:354)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addPackage(XmlConfigurationProvider.java:468)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadPackages(XmlConfigurationProvider.java:264)
at org.apache.struts2.config.StrutsXmlConfigurationProvider.loadPackages(StrutsXmlConfigurationProvider.java:111)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:193)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:55)
... 31 more