如何读取pdf文件并将其写入outputStream [英] How to read pdf file and write it to outputStream
本文介绍了如何读取pdf文件并将其写入outputStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要读取文件路径为C:file.pdf"的 pdf 文件并将其写入 outputStream.最简单的方法是什么?
I need to read a pdf file with filepath "C:file.pdf" and write it to outputStream. What is the easiest way to do that?
@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();
}
}
}
...........................................................................................
...........................................................................................
推荐答案
import java.io.*;
public class FileRead {
public static void main(String[] args) throws IOException {
File f=new File("C:\Documents and Settings\abc\Desktop\abc.pdf");
OutputStream oos = new FileOutputStream("test.pdf");
byte[] buf = new byte[8192];
InputStream is = new FileInputStream(f);
int c = 0;
while ((c = is.read(buf, 0, buf.length)) > 0) {
oos.write(buf, 0, c);
oos.flush();
}
oos.close();
System.out.println("stop");
is.close();
}
}
目前最简单的方法.希望这会有所帮助.
The easiest way so far. Hope this helps.
这篇关于如何读取pdf文件并将其写入outputStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文