我有我的POJO类中的pdf文件位置和pdf文件。我想使用servlet下载PDF文件。请告诉我一些方法来完成它。 File Location =/tmp/SWBC_444Thu May 03 20:01:07 IST 20124366242221752147545.pdf 使用此文件位置我想提示用户将文件下载为pdf。使用servlet下载报告为pdf
这是我的代码。
File file = new File(filePath);
OutputStream responseOutputStream = response.getOutputStream();
response.setContentLength((int)filePath.length());
FileInputStream fileInputStream = new FileInputStream(file);
int size = fileInputStream.available();
byte[] content = new byte[size];
int bytesRead;
while ((bytesRead = fileInputStream.read(content)) != -1)
{
responseOutputStream.write(content, 0, bytesRead);
}
responseOutputStream.flush();
fileInputStream.close();
responseOutputStream.close();
。我读取和生成文件,但打开文件时它是空的。
感谢你..!
你可以查看这篇文章:[JasperReports:从servlet调用报告](http://stackoverflow.com/questions/2399507/jasperreports-calling-report-from-servlet)&[报告下载不提示用户保存(http://stackoverflow.com/questions/6085049/report-download-not-prompting-user-to-save)。 SO上的搜索很好用;) –
File file = new File(filePath); ServletOutputStream servletOutputStream = response.getOutputStream(); ; BufferedInputStream bufferIput = null; FileInputStream fileInputStream = new FileInputStream(file); bufferIput = new BufferedInputStream(fileInputStream); byte [] bBuffer = new byte [fileInputStream.available()]; int nBytes = -1; ((nBytes = bufferIput.read(bBuffer,0,bBuffer.length)) != -1) { servletOutputStream.write(bBuffer,0,nBytes); }问题解决了。您可以使用此代码来阅读PDF文件并下载 – Stephen
您可以发布解决方案作为答案,以帮助其他人 –