文件和参数一起上传到服务器

 /**
     * 带参数上传文件,可以单个也可以多个
     *
     * @param strURL
     * @param params
     * @param files
     * @return
     * @throws IOException
     * @author M.J
     */
    public static String postFiles(String strURL, List<String[]> params, ArrayList<File> files) throws IOException {
        HttpClient httpclient = getNewHttpClient();
        try {
            HttpPost httppost = new HttpPost(strURL);


            httppost.getParams().setParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 15000);
            httppost.getParams().setParameter(HttpConnectionParams.SO_TIMEOUT, 15000);
            MultipartEntity reqEntity = new MultipartEntity();
            for (int i = 0; i < files.size(); i++) {
                File file = files.get(i);
                if (file != null && file.exists()) {
                    FileBody bin = new FileBody(file);
                    reqEntity.addPart(String.valueOf(i + 1), bin);
                }
                long fileLength = file.length();
                float length = (float) (fileLength / 1024.0 / 1024.0);
                Log.i("NetUtil", "第" + i + "张图片大小为:" + length + "kb");
            }


            if (params != null) {
                for (String[] param : params) {
                    Log.i("xxxx", " param[0] = " + param[0]);
                    Log.i("xxxx", " param[1] = " + param[1]);
                    reqEntity.addPart(param[0], new StringBody(param[1], Charset.forName("UTF-8")));
                }
            }


            httppost.setEntity(reqEntity);
            if (httppost.getURI().getRawPath() != null) {
                Log.i("xxx", "getRawPath() ====" + httppost.getURI().getRawPath());
            }
            long end = System.currentTimeMillis();
            Log.i("NetUtil", "开始发送请求的时间:" + end + "ms");
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity resEntity = response.getEntity();
            CookieStore cookieStore = ((DefaultHttpClient) httpclient).getCookieStore();
            if (cookieStore != null && cookieStore.getCookies() != null && cookieStore.getCookies().size() > 0) {
                PccnApp.getInstance().setCookie(cookieStore);
            }
            if (resEntity != null) {
                InputStream is = resEntity.getContent();
                byte[] buffer = new byte[1024];
                int count = 0;
                StringBuilder resultStr = new StringBuilder();
                while ((count = is.read(buffer, 0, 1024)) != -1) {
                    resultStr.append(new String(buffer, 0, count));
                }
                is.close();
                return resultStr.toString();
            }
            return null;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            // FITLog.error(ExceptionUtil.getCrashInfo(e));
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            // FITLog.error(ExceptionUtil.getCrashInfo(e));
        } finally {
            try {
                httpclient.getConnectionManager().shutdown();
            } catch (Exception ignore) {
            }
        }
        return null;

    }













    private static synchronized DefaultHttpClient getNewHttpClient() {
        DefaultHttpClient httpclient;
        try {
            KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
            trustStore.load(null, null);


            SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
            sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);


            HttpParams params = new BasicHttpParams();
            HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
            HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);


            ConnManagerParams.setTimeout(params, 1000);


            SchemeRegistry registry = new SchemeRegistry();
            registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
            registry.register(new Scheme("https", sf, 443));


            ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);


            httpclient = new DefaultHttpClient(ccm, params);
            // HttpClientParams.setCookiePolicy(httpclient.getParams(),
            // CookiePolicy.BROWSER_COMPATIBILITY);
            CookieStore cookieStore = PccnApp.getInstance().getCookie();
            if (cookieStore != null && cookieStore.getCookies() != null && cookieStore.getCookies().size() > 0) {
                httpclient.setCookieStore(cookieStore);
            }
        } catch (Exception e) {
            httpclient = new DefaultHttpClient();
            // HttpClientParams.setCookiePolicy(httpclient.getParams(),
            // CookiePolicy.BROWSER_COMPATIBILITY);
            CookieStore cookieStore = PccnApp.getInstance().getCookie();
            if (cookieStore != null && cookieStore.getCookies() != null && cookieStore.getCookies().size() > 0) {
                httpclient.setCookieStore(cookieStore);
            }
        }


        return httpclient;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值