zoukankan      html  css  js  c++  java
  • JSP转PDF(字节流方式)

    public static String forward(HttpServletRequest request, HttpServletResponse response, String address) {

    	try {
    		//定义字节流
    		final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    		//重写servletOuputStream输出流 为字节流
    		final ServletOutputStream servletOuputStream = new ServletOutputStream() {
    			@Override
    			public void write(int b) throws IOException {
    				byteArrayOutputStream.write(b);
    			}
    		};
    		//装饰打印流
    		final PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(byteArrayOutputStream, "UTF-8"));
    
    		//重写response输出 皆为字节流
    		response = new HttpServletResponseWrapper(response) {
    			public ServletOutputStream getOutputStream() {
    				return servletOuputStream;
    			}
    			public PrintWriter getWriter() {
    				return printWriter;
    			}
    		};
    
    		//执行forward操作,使返回结果写字节流数组中
    		request.getRequestDispatcher(address).forward(request, response);
    
    		//将字节流中的内容太转为字符串 为jsp转成的html,藉由html转pdf工具进行文件生成
    		return new String(byteArrayOutputStream.toByteArray(), "utf-8");
    	} catch (Exception e) {
    		throw new RuntimeException(e);
    	}
    

    }

    运用示例:

  • 相关阅读:
    人月神话阅读笔记
    12周总结
    IOS成长之路-用NSXMLParser实现XML解析
    沟通能力的表达
    IOS中UIScrollView的contentSize、contentOffset和contentInset属性
    Block 的使用时机
    drawAdapter
    drawpoly()函数的用法
    @synchronized(self)
    Swift2
  • 原文地址:https://www.cnblogs.com/caichaoxiang919/p/13139862.html
Copyright © 2011-2022 走看看