首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

Struts2使用注解方式实现文件上载

2012-08-25 
Struts2使用注解方式实现文件下载1.采用xml方式的实现:1)Action:??2.注解方式:1)Action:/** * Copyright (

Struts2使用注解方式实现文件下载

1.采用xml方式的实现:

1)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  * 其它: */@Componentpublic class FileDownloadAction extends BaseAction {/** */private static final long serialVersionUID = 1L;@Resourceprivate 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");}elsedownFileName = 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路径错误。

热点排行
Bad Request.