[jersey]Download file from Remote Server

從其他網站伺服器下載範例


import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/Purchase")
public class File {
    @GET
  @Path("File/{FileName}")
  @Produces(MediaType.APPLICATION_OCTET_STREAM + "; charset=UTF-8")
  public Response getFile2(
          @PathParam("FileName") String FileName
  ) throws MalformedURLException, IOException  {
    //遠端取檔案下載
    URL url = new URL("http://www.example.com/files/" + FileName);

    URLConnection uc = url.openConnection();

    Response.ResponseBuilder response = Response.ok((Object) uc.getInputStream());
    response.header("Content-Disposition", "attachment; filename=" + FileName);

    return response.build();

  }
}

留言

這個網誌中的熱門文章

[java]BIG5 字集判斷

[java]半型轉全型

[javascript]當月第一天、當月最後一天