八、JAVA 实现回调保存示例

public class IndexServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter writer = response.getWriter();
 
        Scanner scanner = new Scanner(request.getInputStream()).useDelimiter("\\A");
        String body = scanner.hasNext() ? scanner.next() : "";
 
        JSONObject jsonObj = (JSONObject) new JSONParser().parse(body);
 
        if((long) jsonObj.get("status") == 2||(long) jsonObj.get("status") == 6)
        {
            String downloadUri = (String) jsonObj.get("url");
 
            URL url = new URL(downloadUri);
            java.net.HttpURLConnection connection = (java.net.HttpURLConnection) url.openConnection();
            InputStream stream = connection.getInputStream();
 
            File savedFile = new File(“保存文档的路径”);
            try (FileOutputStream out = new FileOutputStream(savedFile)) {
                int read;
                final byte[] bytes = new byte[1024];
                while ((read = stream.read(bytes)) != -1) {
                    out.write(bytes, 0, read);
                }
 
                out.flush();
            }
 
            connection.disconnect();
        }
        writer.write("{\"error\":0}");
    }
}