java端将jasper文件生成pdf

流程:http请求获取传入的josn串,解析后放入jasper文件的变量里,生成pdf后返回成功。

service端根据解析的交易号分发请求:

import java.io.BufferedReader;

import java.io.IOException;

import java.io.OutputStream;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import net.sf.json.JSONObject;

import com.epsoft.zsb.common.CreatePdf;

/**

* http请求总入口,根据jyh分发业务处理

*/

public class DoTradeService extends HttpServlet{

CreatePdf pdf = new CreatePdf();

private static final long serialVersionUID = 1L;

static Logger logger = Logger.getLogger(DoTradeService.class); //First step

public DoTradeService() {

        super();

    }

    public void destroy() {

        super.destroy();

    }

    public void init() throws ServletException {

    }

   public void doPost(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

    request.setCharacterEncoding("UTF-8");

        response.setContentType("text/javascript");


    BufferedReader reader = request.getReader(); 

        char[] buf = new char[512]; 

        int len = 0; 

        StringBuffer contentBuffer = new StringBuffer(); 

        while ((len = reader.read(buf)) != -1) { 

            contentBuffer.append(buf, 0, len); 

        } 

        String varin = contentBuffer.toString();

        logger.info("原始传入"+varin);

        JSONObject jobj = JSONObject.fromObject(varin);

        logger.info("json转换后:返回:"+jobj);

    String jyh = null;   

    jyh = jobj.getString("JYH");

    System.out.println("交易号:"+jyh);

    if(jyh!=null||"".equals(jyh)){

    if(jyh.equals("Si00101")){

    String content = pdf.Si00101(jobj);

    logger.info("Si00101:返回:"+content);

    OutputStream out = response.getOutputStream();

            byte[] b = content.getBytes("UTF-8");

            out.write(b);

            out.flush();

            out.close();

    }

    }

    }


    public void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

doPost(request,response);

    }

}

业务处理:


public class CreatePdf {

  public String Si00101(JSONObject jojb){

         JSONObject result = new JSONObject();

         HashMap parameters = new HashMap();

         parameters.put("bm", jojb.getString("bm"));

         List   prtList =  new ArrayList();

         JSONArray ListInfo = JSONArray.fromObject(jojb.getString("ListInfo"));

        for(int i=0;i< ListInfo.size();i++){

             JSONObject job = ListInfo.getJSONObject(i);

            String[] tt = new String[17];

            tt[0] = job.getString("xm");

            prtList.add(tt);

           }

String[] prtNames = new String[] {"xm"};

String[] xm = (String[]) prtList.get(0);

parameters.put("xm0000", xm[5]);

JRDataSource s1 = new ResultDataSource(prtList,prtNames);

File reportFile = new File("D:/zsb\\shbxzgzj.jasper");

JasperReport jasperReport = null;

try {

   jasperReport = (JasperReport) JRLoader.loadObject(reportFile);

  // JasperPrint print = JasperFillManager.fillReport(jasperReport, parameters, new    JREmptyDataSource());

 JasperPrint print = JasperFillManager.fillReport(jasperReport, parameters,s1);

  JasperExportManager.exportReportToPdfFile(print, "D:/zsb\\shbxzgzj.pdf");

  result.put("JYH", "Si00101");

  result.put("RETMSG", 0);

  result.put("ERRMSG", "");

  }catch (Exception e){

  // e.printStackTrace();

  result.put("JYH", "Si00101");

  result.put("RETMSG", -5);

  result.put("ERRMSG",e.getMessage());

  return result.toString();

    }

  return result.toString();

  }

}

使用postma测试,测试json串为:

{  

"bm":"研发部",

"ListInfo": [

      {"xm":"张三"},

      {"xm":"李四"},

      {"xm":"王五"}

]

}

你可能感兴趣的:(java端将jasper文件生成pdf)