1、编写anction,如下:
package cn.com.css.misps.onlinegraph.web.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import cn.com.css.common.action.BaseAction;
import cn.com.css.misps.domain.ServiceCode;
import cn.com.css.misps.domain.ServiceProductCode;
import cn.com.css.misps.domain.TPrCategory;
import cn.com.css.misps.onlinegraph.service.IServiceCodeService;
import cn.com.css.misps.onlinegraph.service.IServiceProductCodeService;
import cn.com.css.misps.onlinegraph.service.ITPrCategoryService;
import cn.com.css.misps.onlinegraph.service.ITProductInfoService;
import cn.com.css.misps.onlinegraph.util.AnalysisMicaps2BytesUtils;
import cn.com.css.misps.onlinegraph.util.DateUtil;
import cn.com.css.misps.onlinegraph.util.FNameUtils;
import cn.com.css.misps.onlinegraph.util.ImageBytesAnalysisUtils;
import cn.com.css.misps.onlinegraph.util.ProductsStorageUtils;
import cn.com.css.misps.onlinegraph.webservice.impl.GraphWebServiceImpl;
import cn.com.system.domain.Account;
import com.opensymphony.xwork2.ActionContext;
/**
* @brief OnlineGraphAction.java 这里是在线出图部分的代码
* @attention 要注意的是页面中的表单填写的是服务方法中要求填写的参数
* @author 涂作权
* @date 2013-9-4
* @note begin modify by null
*/
@SuppressWarnings("serial")
@Scope("prototype")
@Controller
public class OnlineGraphAction extends BaseAction {
// 相对门户项目的相对路径
private String relativeStorageFilePath;
// 图形文件的文件名称
private String imgPath;
// 下载文件用户的
private InputStream inputStream;
/**
* \brief 下载产生的图片
*
* @return
* @attention
* @author 涂作权
* @date 2014-3-26
* @note begin modify by null
*/
public String download() throws Exception {
inputStream = new FileInputStream(
ProductsStorageUtils.graphAbsolutePath
+ relativeStorageFilePath + "/" + imgPath);
//这里的imgPath表示的是的是文件的名称,即:fName
String name = URLEncoder.encode(imgPath,"utf-8");
ActionContext.getContext().put("name", name);
return "download";
}
/**
* @return the imgPath
*/
public String getImgPath() {
return imgPath;
}
/**
* @param imgPath
* the imgPath to set
*/
public void setImgPath(String imgPath) {
this.imgPath = imgPath;
}
/**
* @return the inputStream
*/
public InputStream getInputStream() {
return inputStream;
}
/**
* @param inputStream
* the inputStream to set
*/
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
}
在struts配置文件中进行如下配置:
<action name="onlinegraph_*" class="cn.com.css.misps.onlinegraph.web.action.OnlineGraphAction" method="{1}">
<result name="download" type="stream">
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename="${#name}"</param>
<param name="bufferSize">4096</param>
</result>
</action>