开发者社区> 问答> 正文

struts 文件下载报getOutputStream() has alread?报错

Struts下使用type="stream"配置action,文件下载时报错getOutputStream() has already been called 
在网上找了一下解决办法
http://www.cnblogs.com/zenger1025/archive/2013/02/05/2893219.html
但是它这里使用的不是type="stream"配置action,所以他这里可以使用return null
但是我使用的是type="stream"配置action,所以不能return null
请问这样应该怎么解决呢?
或者如果说不用type="stream"配置action,那么应该怎么配置呢?html页面又如何实现呢?

我在CSDN也发帖询问了,网址是:

http://bbs.csdn.net/topics/390713586?page=1#post-396797319

但是问题还没有解决。。。求大神,,,,


以下是我的struts配置:

<action name="download" class="Actions.FileActions"> 
            <result name="success" type="stream"> 
                <param name="contentType">application/octet-stream</param> 
                <param name="contentDisposition">attachment;filename="${fileFileName}"</param> 
                <param name="inputName">downloadFile</param> 
                <param name="bufferSize">4096</param> 
            </result> 
            <result name="input">/index.jsp</result> 
        </action>
action是这样写的:
public InputStream getDownloadFile() {  
//		System.out.println("downloadfile...");
		File_UploadDaoImpl uploadDaoIpml = null;
		DownloadHistoryDaoImpl historyDaoImpl = null;
		conn = DBCPpool.getConnection();
		uploadDaoIpml = new File_UploadDaoImpl(conn);
		historyDaoImpl = new DownloadHistoryDaoImpl(conn);
		File_Upload file = null;
		try {
			file = uploadDaoIpml.selectFileById(Integer.parseInt(id));
			fileFileName = file.getFileName() + file.getType();
			fileFileName = new String(fileFileName.getBytes(),"iso8859-1");
			
			//登记下载记录
			//从session中获取当前用户
			HttpServletRequest request = ServletActionContext.getRequest();
			HttpSession session = request.getSession();
			User user = (User) session.getAttribute("user");
			
			DownloadHistory downHistory = new DownloadHistory();
			downHistory.setUserName(user.getLoginName());
			downHistory.setFileId(file.getId());
			downHistory.setFileName(file.getFileName());
			downHistory.setFileType(file.getType());
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH🇲🇲ss");
			String downTime = sdf.format(new Date());
			downHistory.setDownTime(downTime);
			
			historyDaoImpl.addHistory(downHistory);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			if(conn!=null)
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
		}
	/*	return ServletActionContext.getServletContext().getResourceAsStream(
	            "/uploadFiles/" + file.getDepartment() + "/" + file.getFileName() + file.getType()); */ 
		return ServletActionContext.getServletContext().getResourceAsStream(
	            file.getFileDir() + "/" + file.getFileName() + file.getType());  
	}
前台使用easyui框架编写,直接一个按钮连接到action下载,使用ajax提交
js代码:
//文件下载 
		function downloadFlie(){
			var rows = $("#dg").datagrid("getSelections");
			if(rows.length <=0){
				$.messager.alert("系统提示","请选择一个文件");
			}else if(rows.length >1){
				$.messager.alert("系统提示","每次只能下载一个文件");
			}else{
				document.getElementById("downId").value = rows[0].id;
				document.downloadFM.submit();
			}
		}
HTML代码:
<form id="downloadFM" name="downloadFM" action="download" method="post" style="display: none;">
    	<input type="hidden" id="downId" name="id" />
    </form>



展开
收起
爱吃鱼的程序员 2020-06-22 12:44:38 474 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    是不是下载的文件被占用了?不是,就一个人早操作,就是点了下载,连续多点几次就会这样了,然后就没反应了,后台报这样的错

    2020-06-22 12:44:55
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载