上传数据到服务器以及从服务器下载数据

1、json格式上传数据

/**
     * 
     * 功能说明:用户登陆操作创建时间:2012-11-7 下午4:03:41
     * 
     * @param name
     *            用户名
     * @param password
     *            密码 void
     * @throws JSONException
     * 
     */
    public static LoginResponse login(String name, String password) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("account", name);
            // 密码3des加密
            jsonObject.put("pwd", TripleDES.getEncString(password,
                    HttpGetConstast.DES_CODE));

            String responseStr = HttpPostUtil
                    .doOaPost(HttpGetConstast.BASE_ADDRESS
                            + HttpGetConstast.CLIENT_LOGIN, jsonObject);
            System.out.println("登陆---->" + responseStr);
            return responseStr == null ? null : (LoginResponse) gson.fromJson(
                    responseStr, new TypeToken<LoginResponse>() {
                    }.getType());
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }
/**
     * 
     * 功能说明:通过httpget访问目标网址 创建时间:2012-11-8 上午9:48:48
     * 
     * @param url
     *            访问地址
     * @param param
     *            访问参数
     * @param connectTimeOut
     *            请求超时时间
     * @param readTimeOut
     *            读取超时时间
     * @return String 服务器返回的json字符串
     * 
     */
    public static String doPost(String url, JSONObject param,
            int connectTimeOut, int readTimeOut) {
        HttpPost request = new HttpPost(url);
        // 先封装一个 JSON 对象
        // 绑定到请求 Entry
        try {
            StringEntity se = new StringEntity(param.toString(), HTTP.UTF_8);
            request.setEntity(se);
            request.addHeader("Content-Type", "application/json");
            request.addHeader("charset", HTTP.UTF_8);
            request.getParams().setParameter(
                    CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeOut);
            // 读取超时
            request.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,
                    readTimeOut);

            // 发送请求
            HttpResponse httpResponse = new DefaultHttpClient()
                    .execute(request);
            // 得到应答的字符串,这也是一个 JSON 格式保存的数据
            String retSrc = EntityUtils.toString(httpResponse.getEntity());
            
            return retSrc;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
原文地址:https://www.cnblogs.com/ct732003684/p/3025817.html