0
我在這個項目中,我從一個web服務收到以下形式的工作:轉換成的StringBuffer PDF文件
%PDF-1.5%����3 0 obj<</Length 1294 /Filter /FlateDecode>>streamx�uV�n#G��W�X,E����$d✂��e�A�Z��íe�P��܊E>���ӧ�~.��ql�v��)�~��;t�l6O��O�����.......
我無法閱讀的PDF格式。 我試圖將這些字節插入一個pdf文件,但它沒有奏效。 我也試圖打開使用webview ...沒有成功。
這裏的一些代碼:
URL url = new URL(LINKTOWEBSERVICE); //Enter URL here
private StringBuffer response;
JSONObject jsonObject = new JSONObject();
jsonObject.put("_id", obj.getLogin());
jsonObject.put("senha", md5(obj.getPassword()));
String mensagem = jsonObject.toString();
//connection with server
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setReadTimeout(10000);
connection.setConnectTimeout(15000);
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setFixedLengthStreamingMode(mensagem.getBytes().length);
connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
connection.setRequestProperty("X-Requested-With", "XMLHttpRequest");
//Opening connection
connection.connect();
//Sending data
OutputStream outputStream = new BufferedOutputStream(connection.getOutputStream());
outputStream.write(mensagem.getBytes());
outputStream.flush();
//Response
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
response = new StringBuffer();
String inputLine;
while ((inputLine = bufferedReader.readLine()) != null) {
response.append(inputLine);
}
bufferedReader.close();
。
public File createPDF(byte[] data) {
// Get the directory for the user's public pictures directory.
String fileName = "exame.pdf";
File pdfFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), fileName);
FileOutputStream outputStream;
byte[] bytes;
try{
if(pdfFile.exists()){
pdfFile.delete();
}
pdfFile.createNewFile();
outputStream = new FileOutputStream(pdfFile);
outputStream.write(String.valueOf(response).getBytes());
outputStream.close();
}
catch (Exception e){
Log.e("PDF CREATION", "FILE NOT CREATED");
e.printStackTrace();
}
它創建PDF文件。但它已損壞。
你能驗證發送的pdf是否有效嗎? –