如何阅读PDF文件并将其写入ByteArrayOutputStream
?
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import net.sf.json.JSONObject;
import org.apache.commons.codec.binary.Base64;
public class NewClass2 {
public static void main(String[] args) {
try {
File pdfFile = new File("C:\\Users\\311001\\Documents\\NetBeansProjects\\JavaApplication1\\src\\javaapplication1\\image\\test_01.pdf");
FileInputStream input = new FileInputStream(pdfFile);
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[65536];
int l;
while ((l = input.read(buffer)) > 0) {
output.write(buffer, 0, l);
}
input.close();
byte[] pdfBytes = output.toByteArray();
String pdfInString = new String(Base64.encodeBase64(pdfBytes));
JSONObject json1 = new JSONObject();
json1.put("command", pdfInString);
System.out.println(json1);
} catch (Exception e) {
}
}
}
答案 0 :(得分:0)
FileInputStream input = new FileInputStream (pdfFile);
ByteArrayOutputStream output = new ByteArrayOutputStream ();
byte [] buffer = new byte [65536];
int l;
while ((l = input.read (buffer)) > 0)
output.write (buffer, 0, l);
input.close ();
byte [] pdfBytes = output.toByteArray ();