Java Struts2下载文件

本文介绍了一种使用Struts2框架实现文件下载的方法。通过配置struts.xml中的下载动作,结合HTML表单与JavaScript来触发文件下载。文章还展示了如何在Action类中设置文件名并读取指定路径下的文件。

html:

<form  name="deleteUserForm" method="post" enctype="multipart/form-data">
///////////
</form>
<button id="download">下载</button>
$().ready(function(){
  $("#download")
	.button()
	.click(
		function(){
			deleteUserForm.action = "downloadFile.action";
			deleteUserForm.submit(); 
		}
	);		
});
struts.xml

<action name="downloadFile" class="userAction" method="downloadFile">  
   <result name="success" type="stream">  
       <param name="contentDisposition">   
          attachment;filename="${fileName}"  
      </param> 
      <param name="inputName">downloadFileStream</param>   
   </result>   
</action> 
UserAction

private String fileName;
public String getFileName() {
	SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-DD");
	String fileName = sf.format(new Date()).toString() + ".xls";
	try {
		fileName = new String(fileName.getBytes(),"utf-8");
	} catch (Exception e) {
		e.printStackTrace();
	}
	return fileName;
}
public void setFileName(String fileName) {
	this.fileName = fileName;
}

public String downloadFile(){
	return SUCCESS;
}
public InputStream getDownloadFileStream(){
	String directory = "/upload";
	String targetDirectory = ServletActionContext.getServletContext().getRealPath(directory);
	try {
		return new FileInputStream(new File(targetDirectory, uploadFileFileName));
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return null;
}

struts.xml 中fileName 对应 action中fileName, downloadStream对应action中 getDownloadStream, get方法首字母小写。

根据路径+文件名,返回文件流。

fileName可直接根据时间设置,也可以根据目录列出文件名获取。

fileList = getfiles(getdbName);  
  
public  String getfiles(String dbDir){  
//       String t=Thread.currentThread().getContextClassLoader().getResource("").getPath();   
//       System.out.println(t);  
//          if(t.indexOf("WEB-INF")>0)  
//              t = t.substring(1,t.indexOf("/WEB-INF/classes"));  
    String t ="C:/DB_Backup";  
    if(dbDir != null && !dbDir.equals("")){  
            t +="/"+dbDir;                
    }  
    String  str = readFile(t);   
    return str;          
}  
public String readFile(String fileName) {   
       String output = "";   
       File file = new File(fileName);   
                 
       if(file.exists()){   
           if(file.isFile()){   
               try{   
                   BufferedReader input = new BufferedReader (new FileReader(file));   
                   StringBuffer buffer = new StringBuffer();   
                   String text;   
                   while((text = input.readLine()) != null)   
                   buffer.append(text +"\n");   
                   output = buffer.toString();                       
            } catch(IOException ioException){   
                  System.err.println("File Error!");   
            }   
        } else if(file.isDirectory()){   
            String[] dir = file.list();   
            //     output += "Directory contents:\n";   
                      
           for(int i=0; i<dir.length; i++){   
                 output += dir[i] +",";   
           }   
        }   
    } else{   
          System.err.println("Does not exist!");   
    }   
    return output;   
}




内容概要:本文提出了一种基于非合作博弈理论的居民负荷分层调度模型,并结合双层鲸鱼优化算法(Two-level Whale Optimization Algorithm)进行高效求解,模型与算法均通过Matlab代码实现。研究针对电力系统中居民侧用电负荷的复杂调度问题,引入非合作博弈机制刻画各用户之间的利益竞争关系,实现负荷的分层优化分配;同时设计双层优化架构,上层优化资源配置,下层模拟用户自主决策行为,提升了模型的实用性与合理性。通过智能优化算法求解多层级、非凸非线性的博弈模型,有效提高了调度方案的收敛性与全局寻优能力,适用于现代智能电网中的需求侧管理与能源优化场景。; 适合人群:具备电力系统基础理论知识和Matlab编程能力,从事智能电网、能源优化调度、需求侧管理、博弈论应用等方向的科研人员、高校研究生及工程技术人员。; 使用场景及目标:①应用于居民区电力负荷的分层优化调度系统设计与仿真分析;②为非合作博弈在多主体能源系统建模中的应用提供方法论支持;③利用双层鲸鱼算法解决具有嵌套结构的复杂双层优化问题,提升求解效率与调度方案的可行性。; 阅读建议:建议读者结合提供的Matlab代码深入理解模型构建逻辑与算法实现流程,重点关注博弈模型的效用函数设计、纳什均衡求解思路以及双层优化结构的迭代机制,宜配合实际用电数据开展复现实验以验证模型有效性与鲁棒性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值