1.采用xml方式的实现:
1)Action:
package frontview.download.action; import java.io.File; import java.io.InputStream; import java.io.UnsupportedEncodingException; import model.FileInfo; import org.apache.struts2.ServletActionContext; import common.BaseAction; import constant.Constant; import frontview.download.service.IFrontDownloadService; /** * 软件下载Action * * @author heweina frontview.download.action.SoftwareDownloadAction.java * 2011-5-5 下午12:59:07 */ public class SoftwareDownloadAction extends BaseAction { /** * */ private static final long serialVersionUID = 1L; private IFrontDownloadService frontDownloadService; //服务器端文件名 private String fileName; //下载类型 private String type; //浏览器类型 private String brower; public IFrontDownloadService getFrontDownloadService() { return frontDownloadService; } public void setFrontDownloadService( IFrontDownloadService frontDownloadService) { this.frontDownloadService = frontDownloadService; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getBrower() { return brower; } public void setBrower(String brower) { this.brower = brower; } /** * 获取下载文件的输入流 * @return 下载文件的输入流 * @throws Exception */ public InputStream getInputStream() throws Exception { //真实路径 String realPath = Constant.FILEFOLODER + fileName; InputStream in = null; try{ in = ServletActionContext.getServletContext().getResourceAsStream(realPath); }catch(Exception ex){ } return in; } /** * 获取下载文件显示名称(客户端名称) * @return 下载文件显示名称 * @throws Exception */ public String getDownloadFileName() throws Exception { String fileID = request.getParameter("fileID"); FileInfo fileInfo = frontDownloadService.getFileClientNameByID(fileID); String downFileName = fileInfo.getFileClientName()+"."+fileInfo.getFileSuffix(); try{ //IE浏览器 if("IE".equals(brower)){ downFileName = java.net.URLEncoder.encode(downFileName,"UTF-8"); }else downFileName = new String(downFileName.getBytes(), "ISO8859-1"); } catch (UnsupportedEncodingException e) { } return downFileName; } @Override public String execute() throws Exception { String forward = null; //真实路径 String realPath = Constant.FILEFOLODER + fileName; String filePath = ServletActionContext.getServletContext().getRealPath(realPath); //文件不存在 if(!new File(filePath).exists()){ //全部列表下载 if("page".equals(type)){ forward = "fileNotFound"; } //首页前六条下载 if("first".equals(type)){ forward = ERROR; } }else{ forward = SUCCESS; } return forward; } }
?
2)struts.xml
<!-- 软件下载 --> <action name="softwareDownload" class="frontview.download.action.SoftwareDownloadAction"> <param name="inputPath">/upload/unknown.doc</param> <!-- 初始文件名 --> <param name="fileName">unknown.doc</param> <result name="success" type="stream"> <!-- 指定下载文件的文件类型,设置输出的类型为二进制流 --> <param name="contentType">application/octet-stream;charset=ISO8859-1</param> <!-- 指定下载文件的文件位置 --> <param name="inputName">inputStream</param> <!-- 使用经过转码的文件名作为下载文件名,downloadFileName属性 对应action类中的方法 getDownloadFileName() --> <param name="contentDisposition">attachment;filename="${downloadFileName}"</param> <!-- 输出时缓冲区的大小 --> <param name="bufferSize">4096</param> </result> <result name="fileNotFound">/frontview/download/fileNotFind.jsp</result> <result name="error">/frontview/download/tip.jsp</result> </action>
?
?2.注解方式:
1)Action:
/** * Copyright (C), 2011-2012, beijing ow Co., Ltd. * 文件名: FileDownloadAction.java */ package com.ow.ovmweb.plugins.servicemanage.servicebrowse.action; import java.io.File; import java.io.InputStream; import java.io.UnsupportedEncodingException; import javax.annotation.Resource; import org.apache.struts2.ServletActionContext; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Result; import org.springframework.stereotype.Component; import com.ow.core.common.BaseAction; import com.ow.ovmweb.plugins.servicemanage.servicebrowse.model.ServiceQueryCondition; import com.ow.ovmweb.plugins.servicemanage.servicebrowse.service.IServiceManageService; import com.ow.tools.PropertiesTools; /** * 类描述: 文件下载的Action * 修改历史: * @author heweina * @version 1.0 * @date 2011-12-1 下午09:53:46 * @description * 其它: */ @Component public class FileDownloadAction extends BaseAction { /** */ private static final long serialVersionUID = 1L; @Resource private IServiceManageService serviceManageService; private ServiceQueryCondition queryCondition; //当前页数 private int curpager; //服务器端文件名 private String fileName; private String orgID; private String serviceID; //浏览器类型 private String brower; public IServiceManageService getServiceManageService() { return serviceManageService; } public void setServiceManageService(IServiceManageService serviceManageService) { this.serviceManageService = serviceManageService; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public String getBrower() { return brower; } public void setBrower(String brower) { this.brower = brower; } public ServiceQueryCondition getQueryCondition() { return queryCondition; } public void setQueryCondition(ServiceQueryCondition queryCondition) { this.queryCondition = queryCondition; } public int getCurpager() { return curpager; } public void setCurpager(int curpager) { this.curpager = curpager; } public String getOrgID() { return orgID; } public void setOrgID(String orgID) { this.orgID = orgID; } public String getServiceID() { return serviceID; } public void setServiceID(String serviceID) { this.serviceID = serviceID; } /** * 获取下载文件的输入流 * @return 下载文件的输入流 * @throws Exception */ public InputStream getInputStream() throws Exception { //真实路径 String fileFolder = PropertiesTools.getValue("com/ow/ovmweb/global/constant.properties", "fileFolder"); String filePath = fileFolder+File.separator+fileName; InputStream in = null; try{ in = ServletActionContext.getServletContext().getResourceAsStream(filePath); }catch(Exception ex){ } return in; } /** * 获取下载文件显示名称(客户端名称) * @return 下载文件显示名称 * @throws Exception */ public String getDownloadFileName() throws Exception { String downFileName = fileName; try{ //IE浏览器 if("IE".equals(brower)){ downFileName = java.net.URLEncoder.encode(downFileName,"UTF-8"); }else downFileName = new String(downFileName.getBytes(), "ISO8859-1"); } catch (UnsupportedEncodingException e) { } return downFileName; } @Override @Action(value="fileDownload",results={ @Result(name=SUCCESS, type="stream", params={"contentType","application/octet-stream;charset=ISO8859-1","inputName","inputStream", "contentDisposition","attachment;filename=\"${downloadFileName}\"","bufferSize","4096"}), @Result(name="fileNotFound", location="/pages/servicemanage/servicebrowse/tip.jsp") }) public String execute() throws Exception { String forward = null; String fileFolder = PropertiesTools.getValue("com/ow/ovmweb/global/constant.properties", "fileFolder"); String filePath = ServletActionContext.getServletContext().getRealPath("")+File.separator+fileFolder+File.separator+fileName; //文件不存在 if(!new File(filePath).exists()){ forward = "fileNotFound"; }else{ forward = SUCCESS; } return forward; } }
?注意:执行过程中可能会出现如下错误:
?不是inputName配置的方法找不到,而是其中的getInputStream()方法 中的代码出现了异常,可以通过 System.out.println(in);
打印出输入流,如果为NULL的话,说明realPath路径正确,否则是这个流的realPath路径错误。