public class DownLoadServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) {
this.doService(request, response);
}
public void doGet(HttpServletRequest request, HttpServletResponse response) {
this.doService(request, response);
}
public void doService(HttpServletRequest request, HttpServletResponse response) {
String fileName = "d:/files/aaa.pdf";
InputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
return;
}
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "inline; filename=" + fileName);
int size;
byte[] buffer = new byte[1024];
try {
while ((size = inputStream.read(buffer)) != -1) {
response.getOutputStream().write(buffer, 0, size);
response.getOutputStream().flush();
}
} catch (IOException e) {
e.printStackTrace();
return;
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e2) {
e.printStackTrace();
return;
}
}
}
}
}
PDF_File_Download
最新推荐文章于 2022-04-12 10:41:15 发布