文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>JSF实现下载功能

JSF实现下载功能

时间:2011-04-22  来源:MISS_MY_GOD

在jsp页面上的代码如下:


<h:commandLink id="file1" value="下载报告的word文档" action="#{ReadTxtOutWord.download}" type="button"> </h:commandLink>

 后台代码:

public class ReadTxtOutWord {

    /**
     * 下载文档
     * @return
     */
    public String download() {
        String path="D:\\";
        //String fileName="outWord.doc";
        try {
        FacesContext ctx = FacesContext.getCurrentInstance();
        ctx.responseComplete();
        String contentType = "application/x-download";

        HttpServletResponse response = (HttpServletResponse) ctx.getExternalContext().getResponse();
        response.setContentType(contentType);
        StringBuffer contentDisposition = new StringBuffer();
        contentDisposition.append("attachment;");
        contentDisposition.append("filename=\"");
        contentDisposition.append(fileName);
        contentDisposition.append("\"");
        response.setHeader("Content-Disposition", new String(contentDisposition.toString().getBytes(System.getProperty("file.encoding")),"iso8859_1"));
       ServletOutputStream out = response.getOutputStream();
        byte[] bytes = new byte[0xffff];
        InputStream is = new FileInputStream(new File(path + fileName));
        int b = 0;
        while ((b = is.read(bytes, 0, 0xffff)) > 0) {
        out.write(bytes, 0, b);
        }
        is.close();
        out.flush();
        ctx.responseComplete();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    private static void outWord()
    {
        Document document = new Document(PageSize.A4);

        try
        {
            RtfWriter2.getInstance(document,new FileOutputStream("E:/outWord.doc"));
            document.open();
            Paragraph ph=new Paragraph("源项数据",new Font(Font.BOLD,20,Font.BOLDITALIC));
            ph.setAlignment(1);
            document.add(ph);
            
             ph=new Paragraph("源项数据1",new Font(Font.BOLD,15,Font.NORMAL));
                ph.setAlignment(1);
                document.add(ph);
            
            RtfHeaderFooter  headers= new RtfHeaderFooter(new Phrase("This header is only on left hand pages"));
            document.setHeader(headers);
            Table table=new Table(4);
            table.setBorder(0);
            table.setBorderWidth(1);   
            table.setBorderColor(Color.BLACK); 
            table.setPadding(0);    
            table.setSpacing(0);  
            
            Cell cell=new Cell("源项名称");
            cell.setHeader(true);
            cell.setBackgroundColor(new Color(210,51,5));
            table.addCell(cell);
            cell=new Cell("释放时间");
            cell.setHeader(true);
            cell.setBackgroundColor(new Color(210,51,5));
            table.addCell(cell);
            table.addCell("释放地点");
            table.addCell("释放计量");
            table.endHeaders();
            
            table.addCell("UF6");
            table.addCell("2011.2.1 12:20:41");
            table.addCell("东京西城区");
            table.addCell("45675.23");
            

            table.addCell("UF6");
            table.addCell("2011.2.1 12:20:41");
            table.addCell("东京西城区");
            table.addCell("45675.23");
            document.add(table);
        
            Chunk chunk = new Chunk("Hello world", FontFactory.getFont(FontFactory.COURIER, 20, Font.ITALIC, new Color(255, 0, 0)));
            document.add(chunk);
            
            Paragraph p1 = new Paragraph(new Chunk("This is my first paragraph.", FontFactory.getFont(FontFactory.HELVETICA, 12)));
            Paragraph p2 = new Paragraph(new Phrase("This is my second paragraph.", FontFactory.getFont(FontFactory.HELVETICA, 12)));
            Paragraph p3 = new Paragraph("This is my third paragraph.", FontFactory.getFont(FontFactory.HELVETICA, 12));
            
            document.add(p1);
            document.add(p2);
            document.add(p3);
            
            document.close();
            System.out.println("文档生成成功!");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally
        {
            
        }
    }
}

 

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

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载