zoukankan      html  css  js  c++  java
  • 开放通知接口,执行文件同步操作

    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();
                }
            }
            
        }
    
    }
  • 相关阅读:
    大话数据结构—散列表查找(哈希表)
    全栈project师?给把瑞士军刀你去砍鬼子好不好!?
    合作开发带来的思考
    女码农献丑-企业智能机器人客服(图灵机器人)
    Elasticsearch聚合 之 Date Histogram聚合
    Elasticsearch聚合 之 Terms
    Elasticsearch聚合初探——metric篇
    AngularJS API之$injector ---- 依赖注入
    AngularJS API之extend扩展对象
    AngularJS API之equal比较对象
  • 原文地址:https://www.cnblogs.com/xzs603/p/2994605.html
Copyright © 2011-2022 走看看