I am writing PDF byte array in response but when I download the file, its showing blank. Means no content. Here what I have tried. How to resolve that. I have written following servlet to read pdf file and then send that pdf file as response but I am getting blank PDF.
@WebServlet("/downloadTest")
public class TestServlet extends javax.servlet.http.HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
performTask(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
performTask(request, response);
}
private void performTask(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
File pdfFile = new File("D://Tempo.pdf");
response.setContentType("application/pdf");
response.addHeader("Content-Disposition", "attachment; filename=" + "Tempo");
response.setContentLength((int) pdfFile.length());
FileInputStream fileInputStream = new FileInputStream(pdfFile);
OutputStream responseOutputStream = response.getOutputStream();
int bytes;
while ((bytes = fileInputStream.read()) != -1) {
responseOutputStream.write(bytes);
}
fileInputStream.close();
}
}
I am getting something like below in response.
%PDF-1.4
%âãÏÓ
3 0 obj
<</Length 708/Filter/FlateDecode>>stream
xTMSÛ0½ûWèHQµú´nup¡ÍôI;Ð3þ}%%4v¼pÈLF»zoßê=?'ã2¥I¹Lò¾FÊ_ÉAFNªü¨%¹ÌȲ¾}y¬Zú¥üí®°ÍMú?2¹JÃSw~0ªÈ_Ï¢!J!IS%ÅæÜ2ü¤ZàãÁ"ÜBGÈ%DÈ¥+#wÜFùs×±£;ºÑ×n¬t£!ºÑÝ8ÑJັó -xÝX!èÆ
A7ZÐò -èyÐÝ/\ù0ñ"uçÆáÝ8ÝÖ/Omó:¿ðáykV@ æl6ñE¤å4Þb¹lªÕj~>íSÔ-á
ñb2!$Hý)Le)S]L>ÄäSc1Ý÷ͺÕÛ)Õ\ÉÈ/í]Ý8ÀaÔn'ÄÖ(ÀͽËpÒÅ`©°[¼lz jIqG÷(´ 2t\'ó<+{© Ú¸o-ErkøY×vV"ÿh%Òµà.oÆ=<#ÖV]Ow>-¼^Gú·ùKQqCD¬ÚE[v$(ùá«*6ù\4"'6N/òiIR®\¿îwgy~=)y?I<ôº'^e)ÝRéd$J(äÉlg½WáÒ-`cAÒ¶¢³E{÷Jûªi»¥þû»;ÄYñí¬ ß{Îåöÿ3,(è¹Ï{:>ⱦÊ%v«8<½Ä¬6sÆÔ¥P<¬nxéøæ4»^G5û~ÊlZîo6¥ö2Û^³u!ß1Û2j¶
j6ÍÙ+WMKÆ>äq«íµÚfÕì`-ûÈjÃ[ïXmؼÕúVû(@qQ
endstream
endobj
5 0 obj
<</Type/Page/MediaBox[0 0 612 792]/Resources<</Font<</F1 1 0 R/F2 2 0 R>>>>/Contents 3 0 R/Parent 4 0 R>>
endobj
1 0 obj
<</Type/Font/Subtype/Type1/BaseFont/Times-Bold/Encoding/WinAnsiEncoding>>
endobj
2 0 obj
<</Type/Font/Subtype/Type1/BaseFont/Times-Roman/Encoding/WinAnsiEncoding>>
endobj
4 0 obj
<</Type/Pages/Count 1/Kids[5 0 R]>>
endobj
6 0 obj
<</Type/Catalog/Pages 4 0 R>>
endobj
7 0 obj
<</Producer(iText® 5.5.10 ©2000-2015 iText Group NV \(AGPL-version\))/CreationDate(D:20170615141342+05'30')/ModDate(D:20170615141342+05'30')/Author(KSHITIJ)/Creator(Rishabh Software)/Title(Title)/Subject(Subject)>>
endobj
xref
0 8
0000000000 65535 f
0000000911 00000 n
0000001000 00000 n
0000000015 00000 n
0000001090 00000 n
0000000790 00000 n
0000001141 00000 n
0000001186 00000 n
trailer
<</Size 8/Root 6 0 R/Info 7 0 R/ID [<8fa4882fe3f73b7d1a30457c18d686a4><8fa4882fe3f73b7d1a30457c18d686a4>]>>
%iText-5.5.10
startxref
1416
%%EOF