• 网络编程,工具类


    /**
         * 从输入流获取数据
         * @throws Exception 
         */
        public static byte[] readInputStram(InputStream inputStream) throws Exception{
            byte[] buffer = new byte[1024];
            int len = -1;
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            while((len = inputStream.read(buffer)) != -1){
                outStream.write(buffer, 0, len);
            }
            outStream.close();
            inputStream.close();
            return outStream.toByteArray();
        }
    /**
         *直接通过http协议提交表单到服务器。
         *FormFile 文件  struts的Formfile(//import org.apache.struts.upload.FormFile;)
         */
        public static String post(String actionUrl, Map<String, String> params, FormFile[] files){
            try {
                String BOUNDARY = "---------7d4a6d158c9";//数据分割线
                String MULTIPART_FORM_DATA = "multipart/form-data";
                URL url = new URL(actionUrl);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setConnectTimeout(5 * 1000);
                conn.setDoInput(true);//允许输入
                conn.setDoOutput(true);//允许输出
                conn.setUseCaches(false);
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Connection", "Keep-Alive");
                conn.setRequestProperty("Charset", "UTF-8");
                conn.setRequestProperty("Content-Type", MULTIPART_FORM_DATA + "; boundary-" + BOUNDARY);
                StringBuilder sb = new StringBuilder();
                for(Map.Entry<String, String> entry : params.entrySet()){
                    sb.append("--");
                    sb.append(BOUNDARY);
                    sb.append("
    ");
                    sb.append("Content-Disposition: form-data; name="" + entry.getKey() + "
    
    ");
                    sb.append(entry.getValue());
                    sb.append("
    ");
                }
                DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());
                outStream.write(sb.toString().getBytes());//发送表单字段
                for(FormFile file : files){
                    StringBuilder split = new StringBuilder();
                    split.append("--");
                    split.append(BOUNDARY);
                    split.append("
    ");
                    split.append("Content-Disposition: form-data;name="" + file.getFormname() + "";filename=""+file.getFilename() + ""
    ");
                    split.append("Content-Type: " + file.getContentType()+"
    
    ");
                    outStream.write(split.toString().getBytes());
                    if(file.getInStream() != null){
                        byte[] buffer = new byte[1024];
                        int len = 0;
                        while((len = file.getInStream().read(buffer)) != -1){
                            outStream.write(buffer, 0, len);
                        }
                        file.getInStream().close();
                    }else{
                        outStream.write(file.getData(), 0, file.getData(),length);
                    }
                    outStream.write("
    ".getBytes());
                }
                byte[] end_data = ("--" + BOUNDARY + "--
    ").getBytes();
                outStream.write(end_data);
                outStream.flush();
                int cah = conn.getResponseCode();
                if(cah != 200)
                    throw new RuntimeException("请求url失败");
                InputStream is = conn.getInputStream();
                int ch;
                StringBuilder b = new StringBuilder();
                while((ch = is.read()) != -1){
                    b.append((char)ch);
                }
                outStream.close();
                conn.disconnect();
                return b.toString();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
  • 相关阅读:
    python中线程 进程 协程
    python 部署lvs
    python中函数
    python监控cpu 硬盘 内存
    python文件操作
    python中字典
    零基础逆向工程34_Win32_08_线程控制_CONTEXT结构
    零基础逆向工程33_Win32_07_创建线程
    零基础逆向工程32_Win32_06_通用控件_VM_NOTIFY
    零基础逆向工程31_Win32_05_提取图标_修改标题
  • 原文地址:https://www.cnblogs.com/chrono/p/4026314.html
Copyright © 2020-2023  润新知