我制作了一个网络服务,它使用 mutlipart/formdata 向客户端发送多个 pdf 作为响应,但碰巧其中一个客户端是不支持 mutlipart/formdata 的 salesforce。
他们想要一个 json 响应,如 - { "filename": xyzname, "fileContent": fileContent }
我尝试使用 apache 编解码器库在 Base64 中编码数据,但客户端的 pdf 似乎已损坏,我无法使用 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-------------- …Run Code Online (Sandbox Code Playgroud)