开放通知接口,执行文件同步操作

View Code
package me.xuzs.sso.web.servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UpdateStaticResource extends HttpServlet {

    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String updateFile = req.getParameter("updateFile");
        String saveFile = "d:/" + updateFile;
        if(!processUpdate(updateFile, saveFile)){
            // log
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        super.doPost(req, resp);
    }

    @Override
    public void init() throws ServletException {
        super.init();
    }
    
    private boolean processUpdate(String url, String downloadFile){
        // TODO 此处读取OM地址
        String fullUrl = "http://xuzs-host.xuzs.com" + url;
        int bytesum = 0;
        int byteread = 0;  
        InputStream inStream = null;
        FileOutputStream fos = null;
        try {
            URL fileUrl = new URL(fullUrl);
            inStream = fileUrl.openStream();
            fos = new FileOutputStream(downloadFile);
            File file = new File(downloadFile);
            if(!file.exists()){
                file.createNewFile();
            }
            byte[] buffer = new byte[1204];
            while ((byteread = inStream.read(buffer)) != -1) {
                bytesum += byteread; 
//                System.out.println(bytesum);
                
                fos.write(buffer, 0, byteread);
            }
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
    }

}
原文地址:https://www.cnblogs.com/xzs603/p/2994605.html