下载文件
来源:互联网 发布:正太捏脸数据 剑网三3 编辑:程序博客网 时间:2024/04/30 10:53
关于打开或者下载各种的文件
package com.zte.matermanage.action;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import com.zte.matermanage.bean.History;
import com.zte.matermanage.common.Constant;
import com.zte.matermanage.common.DataUtil;
import com.zte.matermanage.common.StringUtil;
import com.zte.matermanage.logic.ProductManagerService;
/**
*
* @author xiaoyong
*
*/
public class DownloadAction extends MappingDispatchAction
{
/**
* 用于记录日志
*/
private Logger log = Logger.getLogger(DownloadAction.class) ;
private String filepath = "" ;
String ALLDOWNTYPE = "all" ;
String EXCELTYPE = "excel" ;
String filename = "" ;
String roleid = "" ;
HistoryAction historyAction = null;
public ActionForward execDownload(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
{
String type = request.getParameter("type") ;
if(type.equals(ALLDOWNTYPE))
{
filename = request.getParameter("filename") ;
if(StringUtil.checkNull(filename))
{
return null;
}
filepath = Constant.FILE_PATH + filename ;
}else if(type.equals(EXCELTYPE))
{
File f = new File(Constant.FILE_PATH);
if(!f.exists())
{
f.mkdir();
}
filepath = Constant.FILE_PATH + DataUtil.getFormateDate("yyyyMMddHHmmss") + Constant.EXCEL_FILE_NAME;
filename = Constant.EXCEL_FILE_NAME ;
roleid = request.getParameter("roleid") ;
String excelHeader [] = excelHead();
WritableWorkbook wwb;
try
{
wwb = Workbook.createWorkbook(new File(filepath));
WritableSheet ws = wwb.createSheet("Sheet 1", 0);//sheet1是导出excel分页第1页
for(int i=0;i<excelHeader.length;i++)
{
ws.addCell(new jxl.write.Label(i, 0, excelHeader[i]));//导出后excel中的字段名称,i是列,第一行
ws.setColumnView(i, 20);
}
//从SESSION得到要导出具体值
List<History> listHistory = (List)request.getSession().getAttribute("listHistory") ;
ProductManagerService service = ProductManagerService.getInstance() ;
historyAction = new HistoryAction() ;
if(listHistory.size() > 0)
{
//根据权限,得到查看材料价格的权限
String roleGroup = service.queryRoles(roleid);
int h = 1;
List<History> list = historyAction.historyPriceDisplayPopedom(listHistory,roleGroup) ;
for(int i = 0 ; i < list.size() ;i++)
{
History history = (History)list.get(i);
//只有能被查看价格记录才能被导出
if(!history.getPrice().equals(Constant.PRICE_DISPLAY))
{
ws.addCell(new Label(0,h,history.getModifyUsername()));
ws.addCell(new Label(1,h,history.getData()));
ws.addCell(new Label(2,h,history.getMatr_name()));
ws.addCell(new Label(3,h,history.getPrice()));
ws.addCell(new Label(4,h,history.getRemark()));
ws.addCell(new Label(5,h,history.getAuthorize_user()));
h++ ;
}
}
wwb.write();
wwb.close();
response.reset();
}
}
catch (IOException e)
{
log.error(e) ;
request.setAttribute("errorMsg", Constant.SYSTEM_ERROR) ;
return mapping.findForward("faild") ;
}
catch (RowsExceededException e)
{
log.error(e) ;
request.setAttribute("errorMsg", Constant.SYSTEM_ERROR) ;
return mapping.findForward("faild") ;
}
catch (WriteException e)
{
log.error(e) ;
request.setAttribute("errorMsg", Constant.SYSTEM_ERROR) ;
return mapping.findForward("faild") ;
}
}
//下载指定文件
try
{
BufferedOutputStream bos = null;
StringBuffer sb = new StringBuffer(50);
sb.append("attachment; filename=");
sb.append(filename);
response.setContentType("application/x-msdownload;charset=utf-8");
response.setHeader("Content-Disposition",
new String(sb.toString().getBytes(),"ISO8859-1") );
FileInputStream fis = new FileInputStream(new File(filepath));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buffer = new byte[2048];
int number = 0;
while ((number = fis.read(buffer)) != -1)
{
bos.write(buffer,0,number);
}
fis.close();
bos.close();
} catch (IOException e)
{
log.info(e.getMessage()) ;
request.setAttribute("errorMsg", Constant.SYSTEM_ERROR) ;
return mapping.findForward("faild") ;
}
return null;
}
/**
* EXCEL 表头信息
* @return
*/
public String [] excelHead()
{
return new String []{"申请人","申请时间","物料名称","价格","备注","批注人"};
}
}
- 下载图片,下载文件
- 文件下载
- 文件下载
- 文件下载
- 文件下载
- 文件下载
- 文件下载
- 下载文件
- 文件下载
- 下载文件
- 文件下载
- 文件下载
- 下载文件
- 文件下载
- 下载文件
- 下载文件
- 文件下载
- 下载文件
- 程序员谈如何掌握计算机专业英语
- Eclipse 珍しい書物
- apache里mod_expires的用法
- Windows Presentation Foundation 简介
- 世界上著名的诡异问题~能想出全部的智商至少200!
- 下载文件
- Android开发者必须深入学习的10个应用开源项目
- poj1426
- 文件上传
- Dot NET:嵌入式开发平台(图)
- 正向最大匹配和反向最大匹配
- 电容屏调试和wince6.0的多点触摸。
- 一次db2安装碰到的问题1
- SqlServer2005固定服务器角色