报表报错net.sf.jasperreports.engine.JRException: Error loading object from file

问题描述

Servlet代码privateFileAttachServicefileAttachService=(FileAttachService)AppUtil.getBean("fileAttachService");privateLoglogger=LogFactory.getLog(FilePreviewServlet.class);protectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{StringfileId=req.getParameter("fileId");req.setCharacterEncoding("UTF-8");resp.setCharacterEncoding("UTF-8");if(StringUtils.isNotEmpty(fileId)){FileAttachfileAttach=(FileAttach)this.fileAttachService.get(newLong(fileId));Stringext=fileAttach.getExt();if((ext.toLowerCase().endsWith("doc"))||(ext.toLowerCase().endsWith("docx"))){resp.setContentType("application/msword");}elseif((ext.toLowerCase().endsWith("xls"))||(ext.toLowerCase().endsWith("csv"))){resp.setContentType("application/ms-excel");}elseif(ext.toLowerCase().endsWith("pdf")){resp.setContentType("application/pdf");}elseif(ext.toLowerCase().endsWith("txt")){resp.setContentType("text/plain");}elseif(ext.toLowerCase().endsWith("jpg")){resp.setContentType("image/jpg");}elseif(ext.toLowerCase().endsWith("jpeg")){resp.setContentType("image/jpeg");}elseif(ext.toLowerCase().endsWith("gif")){resp.setContentType("image/gif");}elseif(ext.toLowerCase().endsWith("png")){resp.setContentType("image/png");}elseif(ext.toLowerCase().endsWith("bmp")){resp.setContentType("image/bmp");}else{return;}ServletOutputStreamout=null;try{FileInputStreamfileIn=newFileInputStream(getServletContext().getRealPath("/")+"/attachFiles/"+fileAttach.getFilePath());Stringpath=getServletContext().getRealPath("/")+"/attachFiles/"+fileAttach.getFilePath();this.logger.info("loadfilePath:"+path);System.out.println("loadfilepath"+path);resp.setHeader("Content-Disposition","inline;filename="+URLEncoder.encode(fileAttach.getFileName(),"UTF-8"));out=resp.getOutputStream();byte[]buff=newbyte[1024];intleng=fileIn.read(buff);while(leng>0){out.write(buff,0,leng);leng=fileIn.read(buff);}}catch(Exceptionex){ex.printStackTrace();if(out!=null){try{out.flush();}catch(IOExceptione){e.printStackTrace();}try{out.close();}catch(IOExceptione){e.printStackTrace();}}}finally{if(out!=null){try{out.flush();}catch(IOExceptione){e.printStackTrace();}try{out.close();}catch(IOExceptione){e.printStackTrace();}}}}}JSP代码:request.setCharacterEncoding("utf-8");response.setContentType("text/html;charset=utf-8");response.setCharacterEncoding("utf-8");//查询条件1、条件2Stringcondition1=null;//Stringcondition2=null;//报表类型StringreportType=null;Stringstrid=request.getParameter("reportId");Longid=newLong("0");if(StringUtils.isNotEmpty(strid)){id=Long.parseLong(strid);}ReportParamServicereportParamService=(ReportParamService)AppUtil.getBean("reportParamService");List<ReportParam>plist=reportParamService.findByRepTemp(id);MapvalueMap=request.getParameterMap();MaptypeMap=newHashMap();Mapparameters=newHashMap();SimpleDateFormatformat=newSimpleDateFormat();for(ReportParamp:plist){typeMap.put(p.getParamKey(),p.getParamType());}Iteratorit=valueMap.entrySet().iterator();while(it.hasNext()){Map.Entryentry=(Map.Entry)it.next();Stringkey=(String)entry.getKey();String[]values=(String[])entry.getValue();Stringvalue=URLDecoder.decode(values[0],"UTF-8");ObjectparamType=typeMap.get(key);if(paramType!=null){StringparamTypeStr=paramType.toString();if(paramTypeStr.equals("datefield")){format.applyPattern("yyyy-MM-dd");DatedateValue=format.parse(value);parameters.put(key,newjava.sql.Time(dateValue.getTime()));}elseif(paramTypeStr.equals("datetimefield")){format.applyPattern("yyyy-MM-ddHH:mm:ss");DatedateValue=format.parse(value);parameters.put(key,newjava.sql.Time(dateValue.getTime()));}elseif(paramTypeStr.equals("numberfield")){if(value!=null&&!value.equals("")){DoubledoubleValue=Double.parseDouble(value);parameters.put(key,doubleValue);}else{parameters.put(key,"%");}}else{if(value!=null&&!value.equals("")){parameters.put(key,value);}else{parameters.put(key,"%");}}}else{parameters.put(key,value);}}//根据页面传来的reportId查找上传后的报表模版路径StringreportPath="";StringreportName="";ReportTemplateServicereportTemplateService=(ReportTemplateService)AppUtil.getBean("reportTemplateService");ReportTemplatereportTemplate=reportTemplateService.get(id);reportPath=reportTemplate.getReportLocation();reportName=reportTemplate.getTitle();//Mapparameters=newReportFilter((HttpServletRequest)request).getVariables();//现在报表模版只设置了一个参数,这里也暂时只接受一个参数//condition2=request.getParameter("condition2");//if(condition2==null){//parameters.put("noticeId",0);//}else{//parameters.put("noticeId",condition2);//}/*要是报表中用到了图片的话,在这里设置图片路径参数为相对路径,把图片放在项目中第一个参数是在iReport中设置的图片参数名字,第二个参数是图片所在的项目目录*///parameters.put("REPORT_LOGO_DIR","report\jasper\");reportType=request.getParameter("reportType");StringrootPath=application.getRealPath("/attachFiles");//报表模版附件上传后的根目录try{DataSourcedataSource=(DataSource)AppUtil.getBean("dataSource");Connectionconn=dataSource.getConnection();FilefullPath=newFile(rootPath+"/"+reportPath);//上传后的报表模板的全路径//将解析完的参数传入报表模板中并生成报表//如果接收到的参数为pdf类型的话,则生成pdf的报表System.out.println(fullPath.getPath()+","+parameters);if("pdf".equals(reportType)){byte[]bytes=JasperRunManager.runReportToPdf(fullPath.getPath(),parameters,conn);//设置报表生成类型为PDFresponse.setContentType("application/pdf;charset=utf-8");request.setCharacterEncoding("utf-8");response.setCharacterEncoding("utf-8");response.setContentLength(bytes.length);reportName=reportName+".pdf";response.setHeader("Content-Disposition","attachment;filename="+newString(reportName.getBytes("gb2312"),"ISO8859-1"));ServletOutputStreamouputStream=response.getOutputStream();ouputStream.write(bytes,0,bytes.length);ouputStream.flush();ouputStream.close();conn.close();out.clear();out=pageContext.pushBody();}elseif("xls".equals(reportType)){//如果接受到的参数为xls(excel类型)的话,则生成xls类型的报表JRXlsExporterexporter=newJRXlsExporter();ByteArrayOutputStreamoStream=newByteArrayOutputStream();JasperPrintjasperPrint=JasperFillManager.fillReport(fullPath.getPath(),parameters,conn);exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,oStream);exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,Boolean.TRUE);exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,Boolean.FALSE);exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,Boolean.FALSE);exporter.exportReport();byte[]bytes=oStream.toByteArray();//设置报表生成类型为excelresponse.setContentType("application/vnd.ms-excel");request.setCharacterEncoding("utf-8");response.setCharacterEncoding("utf-8");response.setContentLength(bytes.length);reportName=reportName+".xls";response.setHeader("Content-Disposition","attachment;filename="+newString(reportName.getBytes("gb2312"),"ISO8859-1"));ServletOutputStreamouputStream=response.getOutputStream();ouputStream.write(bytes,0,bytes.length);ouputStream.flush();ouputStream.close();conn.close();out.clear();out=pageContext.pushBody();}else{System.out.println("jspparameters="+parameters);//否则生成html类型的报表JRHtmlExporterexporter=newJRHtmlExporter();ByteArrayOutputStreamoStream=newByteArrayOutputStream();System.out.println("fullPath.getPath()="+fullPath.getPath());System.out.println("jspparameters="+parameters);System.out.println("conn="+conn);JasperPrintjasperPrint=JasperFillManager.fillReport(fullPath.getPath(),parameters,conn);//这里也是解决HTML报表图片不显示的session.setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE,jasperPrint);exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,Boolean.FALSE);//下面这一句是解决HTML报表图片不显示的问题,注意URI得写对,这里URI是指jasper文件所在的目录exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI,"jasper?"+"time="+(newDate()).toString()+"&image=");exporter.setParameter(JRHtmlExporterParameter.JASPER_PRINT,jasperPrint);exporter.setParameter(JRHtmlExporterParameter.CHARACTER_ENCODING,"utf-8");exporter.setParameter(JRHtmlExporterParameter.OUTPUT_STREAM,oStream);exporter.exportReport();byte[]bytes=oStream.toByteArray();//设置报表生成类型的htmlresponse.setContentType("text/html;charset=utf-8");request.setCharacterEncoding("utf-8");response.setCharacterEncoding("utf-8");response.setContentLength(bytes.length);ServletOutputStreamouputStream=response.getOutputStream();ouputStream.write(bytes,0,bytes.length);ouputStream.flush();ouputStream.close();conn.close();out.clear();out=pageContext.pushBody();}}catch(Exceptionex){ex.printStackTrace();}每次执行展示操作JasperPrintjasperPrint=JasperFillManager.fillReport(fullPath.getPath(),parameters,conn);这个地方就报错

时间: 2024-09-19 20:31:05

报表报错net.sf.jasperreports.engine.JRException: Error loading object from file的相关文章

异常-【ActiveReports问题】查看报表报错

问题描述 [ActiveReports问题]查看报表报错 "/"应用程序中的服务器错误. Cyclic report parameter references. 说明: 执行当前 Web 请求期间,出现未经处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息. 异常详细信息: GrapeCity.ActiveReports.Expressions.ExpressionObjectModel.ReportParameterException: Cyclic

error-birt报表报错,请问如何解决?

问题描述 birt报表报错,请问如何解决? 15:18:18,234 ERROR [STDERR] org.eclipse.birt.report.exception.ViewerException: 尝试启动报表引擎时出现致命错误. 15:18:18,234 ERROR [STDERR] at org.eclipse.birt.report.service.ReportEngineService.setEngineContext(Unknown Source) 15:18:18,234 ERR

【POI】解析xls报错:java.util.zip.ZipException: error in opening zip file

今天使用POI解析XLS,报错如下: Servlet.service() for servlet [rest] in context with path [/cetBrand] threw exception [Request processing failed; nested exception is org.apache.poi.openxml4j.exceptions.InvalidOperationException: Can't open the specified file: 'd:

阿泰 C#区的高手让我来请教 报表报错问题

问题描述 使用VS2010水晶报表导出报错错误信息如下system.runtime.InteropServices.SEHException{"外部组件发生异常"}ErrorCode-2147467259stackTrace:在System.Windows.Forms.UnsafeNativeMethods.ThemingScope.DeactivateActCtx(Int32dwFlags,IntPtrlpCookie)在System.Windows.Forms.UnsafeNati

急!打开水晶报表报错

问题描述 点击打印出现"SeagateCrystalReportsActiveXDesigner错误'80043ac6'Accessdenied."之前是可以显示的,但修改.rpt文件后就会弹出错误.急需大神求助 解决方案

RDLC 报表报错!!本机测试没问题,发布网站放到IIS上面跑就报这样的错,具体开发工具:VS2010+SQLSERVER2008R2详细报错见下图

问题描述 解决方案 解决方案二:新IIS服务器安装了RDLC控件么?

Tomcat打报表报错NullPointerException.求解

问题描述 以下是错误提示:MissingPermissionsmanifestattributefor:http://localhost:8080/ZktrReport/ReportPrint.jarMissingCodebasemanifestattributefor:http://localhost:8080/ZktrReport/ReportPrint.jarMissingApplication-Name:manifestattributefor:http://localhost:8080

android建表报错no such column: touxiang

问题描述 android建表报错no such column: touxiang 新加了一个iIMAGE以后数据库报错,只有图中这么一行错 下面是我数据库的代码 package com.mlchelper.db; import java.sql.Blob; import com.mlchelper.dom.Infor_table; import android.content.ContentValues; import android.content.Context; import androi

用ssh写的一个小的登陆网页老报这个错是什么原因?怎么解决?新手求助!请多多帮助!

问题描述 用ssh写的一个小的登陆网页老报这个错是什么原因?怎么解决?新手求助!请多多帮助! 2015-9-18 13:15:20 org.apache.catalina.core.AprLifecycleListener init信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.l