文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>JSP页面导出文件代码

JSP页面导出文件代码

时间:2010-06-21  来源:tessykandy

public ActionForward exportFile(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        String natLog = "test";
                
        try {
            StringBuilder fileName = null;
            OutputStream output = null;
            
            byte[] streamBytes = natLog.getBytes();
            if (streamBytes == null) {

            } else {
                fileName = new StringBuilder(64);
                fileName.append("export.txt");
                try {
                    output = response.getOutputStream();
                    
                    FileStream.initResponseAsFile(response, fileName.toString(), "MIME/octet-strea");
                    output.write(streamBytes);
                    output.flush();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (output != null) {
                        output.close();
                    }
                }
            }
        }
        catch(Exception e) {
            e.printStackTrace();
            return null;
        }
        
        return null;
    }

 

public static void initResponseAsFile(HttpServletResponse resp,    String fileName, String fileType)
            throws UnsupportedEncodingException {
        
        String[] names = fileName.split("\\.");
        String filename = "attachment; filename="
                + new String(names[0].getBytes("GBK"), "ISO8859_1") + "."
                + names[1];
        resp.setCharacterEncoding("charset=GBK");
        resp.setHeader("Content-Disposition", filename); //设定输出文件头


        //定义输出类型

        if (fileType.equals("excel")) {
            resp.setContentType("application/vnd.ms-excel");
        }
        else if(fileType.equals("xml")) {
            resp.setContentType("application/vnd.ms-xml");
        }
        else if(fileType.equals("txt")) {
            resp.setContentType("text/plain");
        }
        else if(fileType.equals("csv")){
            resp.setContentType("application/CSV");
        }
        else if(fileType.equals("MIME/octet-strea")) {
            resp.setContentType("text/plain");
        }
        else {
            resp.setContentType("application/vnd.ms-excel");
        }
            
        
    }


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载