主要透過Response.setOther這個方法就可以簡單達到,
不過要注意使用URI.create下載的連結時,
要避免不合法的URI格式,可用URLEncode.encode解決。
不過會遇到空白檔案下載變+號的編碼,記得取代一下+為%20即可
(謎之聲:The URLEncoder implements the HTML Specifications for how to encode URLs in HTML forms.)。 URLEncoder.encode(obj_name).replace("+", "%20")
如果炸掉可以將回傳結果轉換成json輸出,請看exception的程式範例
@GET
@Path("/download/{store_type}/{store_id}/{uuid}/{obj_name}")
public Response downloadObject(@Context HttpServletRequest request,
@Context HttpServletResponse response,
@PathParam(value = "store_type") String store_type,
@PathParam(value = "store_id") String store_id,
@PathParam(value = "uuid") String uuid,
@PathParam(value = "obj_name") String obj_name,
@QueryParam(value = "alf_ticket") String alf_ticket){
//...省略很多
String downloadlink = "http://test.jpg";
URI reidrectURI = null;
try{
reidrectURI = URI.create(downloadlink );
return Response.seeOther(reidrectURI).build();
}catch(IllegalArgumentException illaEx){
illaEx.printStackTrace();
JSONObject resp = new JSONObject();
resp.put("statuscode", 500);
resp.put("link", originalAlfDownloadLink);
ResponseBuilder builder =
Response.status(Response.Status.INTERNAL_SERVER_ERROR).type(MediaType.APPLICATION_JSON);
builder.entity(resp.toString());
Response respBuilder = builder.build();
return respBuilder;
}
}
沒有留言:
張貼留言
留個話吧:)