I have a really strange issue and I cannot find the solution.
I have a simple test servlet that stream a small pdf file in the response:
public class TestPdf extends HttpServlet implements Servlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
File file = new File(getServletContext().getRealPath("/lorem.pdf"));
response.setContentType("application/pdf");
ServletOutputStream out = response.getOutputStream();
InputStream in = new FileInputStream(file);
byte[] bytes = new byte[10000];
int count = -1;
while ((count = in.read(bytes)) != -1) {
out.write(bytes, 0, count);
}
in.close();
out.flush();
out.close();
}
}
If I call the servlet url with a browser, curl, wget, everything is fine, but when I call it with a simple TCL script like this:
#!/usr/bin/tclsh8.5
package require http;
set testUrl "http://localhost:8080/test/pdf"
set httpResponse [http::geturl "$testUrl" -channel stdout]
the file has a "2000" string at the beginning that corrupt the pdf.
The issue does not seems related to Tomcat or JDK version, since I am able to reproduce it on my development environment (Ubuntu 16.04) with both JDK 1.5.0_22 Tomcat 5.5.36 and JDK 1.8.0_74 and Tomcat 8.5.15.
I have never used TCL but this is the way how you can wtite a general file download servlet:
Hope that this piece of code helps you.