2016-11-24 61 views
0

㈣被要求使用來自於java.The問題webservice的一個PDF是我不知道怎麼寫上的文件,以便它可以通過PDF很好地看出觀衆。消耗和web服務用Java編寫的PDF

URL url = new URL("http://localhost:9090/xcvbb/rest/integrationservices/getPDF"); 
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
conn.setRequestMethod("POST"); 
conn.setRequestProperty("Accept", "application/pdf"); 

if (conn.getResponseCode() != 200) { 
    throw new RuntimeException("Failed : HTTP error code : " 
      + conn.getResponseCode()); 
} 

BufferedReader br = new BufferedReader(new InputStreamReader(
       (conn.getInputStream()))); 

      //writing the downloaded data into the file we created 
      FileOutputStream fileOutput = new FileOutputStream("C:/Users/dkimigho/Downloads/bitarraypdf.pdf"); 

      String output; 
      System.out.println("Output from Server2 .... \n"); 
      while ((output = br.readLine()) != null) { 

       fileOutput.write(br.readLine().getBytes()); 
      } 

      //closed the output stream 
      fileOutput.close(); 
      /// 
      conn.disconnect(); 

      } catch (MalformedURLException e) { 

      e.printStackTrace(); 

      } catch (IOException e) { 

      e.printStackTrace(); 

      } 

對此的任何幫助將不勝感激。

+0

從HttpURLConnection對象獲取輸入流並寫入到具有pdf擴展名的文件。 –

+0

你可能應該看看iText或其他pdf庫 – 2016-11-24 16:49:13

+0

溼婆我已經嘗試過,但它不工作。一個代碼的例子可能會有所幫助,謝謝。 – Kimigx

回答

1

我認爲你應該使用二進制I/O來防止文件被損壞。 您不需要使用任何庫來從任何來源複製文件(按原樣)。

URL url = new URL("http://localhost:9090/xcvbb/rest/integrationservices/getPDF"); 
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
conn.setRequestMethod("POST"); 
conn.setRequestProperty("Accept", "application/pdf"); 

if (conn.getResponseCode() != 200) { 
throw new RuntimeException("Failed : HTTP error code : " 
     + conn.getResponseCode()); 
} 

InputStream is = conn.getInputStream(); 

//writing the downloaded data into the file we created 
FileOutputStream fileOutput = new FileOutputStream("C:/Users/dkimigho/Downloads/bitarraypdf.pdf"); 

/* use binary I/O to prevent line based operation messing with the encoding.*/ 
byte[] buf = new byte[2048]; 
int b_read = 0; 
while ((b_read = is.read(buf)) > 0) { 
    fileOutput.write(buf, 0, b_read); 
} 
fileOutput.flush(); 
//closed the output stream 
fileOutput.close(); 
// 
conn.disconnect(); 

} catch (MalformedURLException e) { 
    e.printStackTrace(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 
+0

感謝buddy它的工作 – Kimigx