2016-06-10 17 views
1

我已經做了一個web服務,發送多個pdf作爲響應客戶端使用mutlipart/formdata,但事實上,其中一個客戶端是salesforce,它不支持mutlipart/formdata。發送pdf數據通過休息api裏面json

他們希望有一個JSON像響應 - { 「文件名」:xyzname, 「fileContent」:fileContent }

我使用Apache的編解碼器庫,但在客戶端PDF似乎得到試圖Base64編碼數據損壞,我無法使用acrobat打開它。

請找到下面的代碼 -

import org.apache.commons.io.FileUtils; 
//------Server side ---------------- 
@POST 
@Consumes(MULTIPART_FORM_DATA) 
@Produces(MediaType.APPLICATION_JSON) 
@Path("somepath") 
public Response someMethod(someparam) throws Exception 
{ 
.... 
JSONArray filesJson = new JSONArray(); 
String base64EncodedData =  Base64.encodeBase64URLSafeString(loadFileAsBytesArray(tempfile)); 
JSONObject fileJSON = new JSONObject(); 
fileJSON.put("fileName",somename); 
fileJSON.put("fileContent", base64EncodedData); 
filesJson.put(fileJSON); 
.. so on ppopulate jsonArray... 
//sending reponse 
responseBuilder = Response.ok().entity(filesJson.toString()).type(MediaType.APPLICATION_JSON_TYPE) ; 
response = responseBuilder.build(); 
} 

//------------Client side-------------- 

Response clientResponse = webTarget.request() 
      .post(Entity.entity(entity,MediaType.MULTIPART_FORM_DATA)); 
String response = clientResponse.readEntity((String.class)); 
JSONArray fileList = new JSONArray(response); 
for(int count= 0 ;count< fileList.length();count++) 
{ 
JSONObject fileJson = fileList.getJSONObject(count);   
byte[] decodedBytes = Base64.decodeBase64(fileJson.get("fileContent").toString()); 
outputFile = new File("somelocation/" + fileJson.get("fileName").toString() + ".pdf");      
FileUtils.writeByteArraysToFile(outputFile,  fileJson.get("fileContent").toString().getBytes()); 
} 

------------------------------- 

請告知。

回答

0

我會從使用「安全」改爲使用「字符串」。所以改變: encodeBase64URLSafeString(...) 到: encodeBase64String(...)

的原因是「安全」的版本實際上改變對內容進行加密之前保存的網址 - 我完全不確定是什麼會做PDF,但懷疑它是你的問題的來源。

如果這不適合你,我建議在服務器(或單獨的測試應用程序)上進行加密/解密,並在嘗試解決問題時比較結果。這樣你就可以看到你在做什麼工作,但是不必每次都要經歷整個「啓動服務器,啓動客戶端,連接...」過程,這會加快你的調試速度。

+0

感謝resposne。 1.我嘗試了encodeBase64String,它沒有工作。 2.我會在eclipse中構建一些本地代碼來測試,而不是客戶端/ api – rasty

+0

因此,我嘗試了在本地進行編碼和解碼工作,因此問題可能是rest api傳輸數據的方式。 ----------------------------- File inputFile = new File(「some pdf」); 字符串base64EncodedData = Base64。encodeBase64String(loadFileAsBytesArray(partiaFile)); //解碼數據 文件decodeFile = new File(「some other pdf」); byte [] decodedBytes = Base64.decodeBase64(base64EncodedData); writeByteArraysToFile(decodedFile,decodedBytes); – rasty

+0

當然 - 或客戶。 如果您可以使用Postman調用Webservice,請嘗試使用Linux coreutils將結果字符串解碼爲文件,然後嘗試使用PDF查看器打開。這將告訴你它是否是其餘的REST編碼或客戶端處理它的方式。對不起,我不是更直接的幫助,這是一個有趣的問題。 –

0

是的,問題出在客戶端。

解碼時我們應該使用

byte [] decodedBytes = Base64.decodeBase64(fileJson.getString(「fileContent」));

而不是
byte [] decodedBytes = Base64.decodeBase64(fileJson.get(「fileContent」)。toString());

由於編碼data.toString()產生一些想象還有


也換成encodeBase64String 好一個非常簡單的解決方案:)

0

在我的REST應用與PHP encodeBase64URLSafeString:

1。將base64 $data = base64_encode($data)中編碼的數據發送到REST。

2.在寫入文件之前,我解碼了$data = base64_decode($data)

3.因此,當文件被下載時,它已經在正確的格式