java - dofilter not giving pdf as output on response -
i trying show pdf output on browser. here code in dofilter class of servlet. getting byte array renders pdf correctly on line --> byte[] pdfarray = pdfconverter.converttodoc(bytes);
see pdf file saved in --> file somefile = new file("c:\\log\\java2.pdf");
it's not outputting on servletresponse, mixed characters show up. appreciated. please inside dofilter
method. have pdfconverter take whole site byte array , convert pdf.
import java.io.bytearrayoutputstream; import java.io.ioexception; import java.io.printwriter; import javax.servlet.filter; import javax.servlet.filterchain; import javax.servlet.filterconfig; import javax.servlet.servletexception; import javax.servlet.servletoutputstream; import javax.servlet.servletrequest; import javax.servlet.servletresponse; import javax.servlet.writelistener; import javax.servlet.http.httpservletresponse; import javax.servlet.http.httpservletresponsewrapper; import org.slf4j.logger; import org.slf4j.loggerfactory; public class itextfilter implements filter{ private static final logger logger = loggerfactory.getlogger(itextfilter.class); private filterconfig filterconfig = null; private string encoding; private static class bytearrayservletstream extends servletoutputstream{ bytearrayoutputstream baos; bytearrayservletstream(bytearrayoutputstream baos){ this.baos = baos; } public void write(int param) throws ioexception{ baos.write(param); } @override public boolean isready() { // todo auto-generated method stub return true; } @override public void setwritelistener(writelistener paramwritelistener) { // todo auto-generated method stub } } private static class bytearrayprintwriter{ private bytearrayoutputstream baos = new bytearrayoutputstream(); private printwriter pw = new printwriter(baos); private servletoutputstream sos = new bytearrayservletstream(baos); public printwriter getwriter(){ return pw; } public servletoutputstream getstream(){ return sos; } byte[] tobytearray(){ return baos.tobytearray(); } } public class charresponsewrapper extends httpservletresponsewrapper{ private bytearrayprintwriter output; private boolean usingwriter; public charresponsewrapper(httpservletresponse response){ super(response); usingwriter = false; output = new bytearrayprintwriter(); } public byte[] getbytearray(){ return output.tobytearray(); } @override public servletoutputstream getoutputstream() throws ioexception{ // error out, if in use if (usingwriter) { super.getoutputstream(); } usingwriter = true; return output.getstream(); } @override public printwriter getwriter() throws ioexception{ // error out, if in use if (usingwriter) { super.getwriter(); } usingwriter = true; return output.getwriter(); } public string tostring(){ return output.tostring(); } } @override public void init(filterconfig filterconfig) throws servletexception { this.filterconfig = filterconfig; this.encoding = filterconfig.getinitparameter("encoding"); } public void destroy() { this.filterconfig = null; } protected string selectencoding(servletrequest request) { return (this.encoding); } public void dofilter(servletrequest request, servletresponse response, filterchain chain) throws ioexception, servletexception { string encoding = selectencoding(request); if (encoding != null) request.setcharacterencoding(encoding); httpservletresponse httpservletresponse = (httpservletresponse) response; charresponsewrapper wrappedresponse = new charresponsewrapper((httpservletresponse)response); logger.info("itextfilter invoked...passing on chain"); chain.dofilter(request, wrappedresponse); logger.info("chain filter complete...processing respose now"); logger.info("response content type chain is: " + wrappedresponse.getcontenttype()); byte[] bytes = wrappedresponse.getbytearray(); pdfconverter pdfconverter = new pdfconverter(); byte[] pdfarray = pdfconverter.converttodoc(bytes); httpservletresponse.setcontenttype("application/pdf; charset=utf-8"); httpservletresponse.setheader("content-disposition", "attachment; filename=\"report.pdf\""); httpservletresponse.addheader("cache-control", "-1"); httpservletresponse.setcontenttype("application/pdf"); httpservletresponse.getoutputstream().write(pdfarray); httpservletresponse.getoutputstream().close(); } }
i found solution. coming gargled (mixed characters) because top of jsp page, changed page contenttype "text/html; charset=utf-8"
"application/pdf; charset=utf-8"
Comments
Post a Comment