ファイルパス「C:\file.pdf」の pdf ファイルを読み取り、outputStream に書き込む必要があります。それを行う最も簡単な方法は何ですか?
@Controller
public class ExportTlocrt {
@Autowired
private PhoneBookService phoneBookSer;
private void setResponseHeaderTlocrtPDF(HttpServletResponse response) {
response.setContentType("application/pdf");
response.setHeader("content-disposition", "attachment; filename=Tlocrt.pdf" );
}
@RequestMapping(value = "/exportTlocrt.html", method = RequestMethod.POST)
public void exportTlocrt(Model model, HttpServletResponse response, HttpServletRequest request){
setResponseHeaderTlocrtPDF(response);
File f = new File("C:\\Tlocrt.pdf");
try {
OutputStream os = response.getOutputStream();
byte[] buf = new byte[8192];
InputStream is = new FileInputStream(f);
int c = 0;
while ((c = is.read(buf, 0, buf.length)) > 0) {
os.write(buf, 0, c);
os.flush();
}
os.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
................................................................... ...................................................