/** * 上传文件 * * @param param * @return */ public int upLoadFile(HttpRequestParam param) { // 创建请求响应的返回对象 final HttpRequestResult httpRequestResult = createHttpRequestResult(param); int handle = createHttpHandle(param, httpRequestResult); // 如果创建Handle失败,则直接返回请求失败 if (httpRequestResult.getStatus() == HttpRequestStatus.FAILED) { return -1; } pool.put(handle, param); Logger.beginDebug(EspaceService.TAG).p("HttpManager:pool:" + pool).end(); int httpRet = httpUploadFile( handle, param.getMethod(), param.getUrl(), param.getBody(), param.getFilePath()); // 发送失败 if (HttpParam.EN_HTTP_ERRORCODE.EN_HTTP_OK != httpRet) { pool.remove(handle); Logger.beginDebug(EspaceService.TAG) .p("HttpManager:SENDREQUEST ERROR:httpRet:" + httpRet) .end(); httpRequestResult.setStatus(HttpRequestStatus.FAILED); return -1; } return handle; }
/** * 方法名称:httpUploadFile 作者: zhang dong 00199735 创建时间:2012-08-30 方法描述:上传文件 输入参数:filepath 文件路径 备注: */ private int httpUploadFile(int handle, String method, String url, String body, String filePath) { StringBuffer strParam = new StringBuffer(); strParam.append("<httpsdk>"); strParam.append("<handle>").append(handle).append("</handle>"); strParam.append("<method>").append(method).append("</method>"); strParam.append("<url>").append(url).append("</url>"); strParam.append("<heads>"); // strParam.append("<Content-Type>").append("text/xml") // .append("</Content-Type>"); // strParam.append("<Cache-Control>").append("non-cache") // .append("</Cache-Control>"); // strParam.append("<User-Agent>") // .append("Mozilla/4.0 (compatible; MSIE 60; Windows.NT)") // .append("</User-Agent>"); // strParam.append("<Accept-Encoding>").append("gzip, deflate") // .append("</Accept-Encoding>"); strParam.append("<Connection>").append("Keep-Alive").append("</Connection>"); // strParam.append("<Accept>").append("*/*").append("</Accept>"); strParam.append("</heads>"); // strParam.append("<body>").append(body).append("</body>"); strParam.append("<filepath>").append(filePath).append("</filepath>"); strParam.append("<needprogress>").append("1").append("</needprogress>"); strParam.append("</httpsdk>"); int iRet = httpSdk.HttpUploadFile(strParam.toString()); Logger.beginDebug(EspaceService.TAG).p("HttpManager:httpUploadFile:" + iRet).end(); return iRet; }
/** * 创建请求Handle, 有可能失败 * * @param param * @param httpRequestResult * @return */ private int createHttpHandle(HttpRequestParam param, final HttpRequestResult httpRequestResult) { if (!init) { httpInit(HttpParam.EN_HTTP_LOG_LEVEL.EN_HTTP_LOG_DEBUG, 10, logPath, 50); } String strRet = httpCreateHandle( param.getIpAddress(), param.getPort(), param.getTlsMode(), param.getAuthMode(), param.getAccount(), param.getPassword(), param.getFullnumber()); int iRet = StringUtil.findIntElement( strRet, "<ret>", "</ret>", HttpParam.EN_HTTP_ERRORCODE.EN_HTTP_OK); // 是否创建成功 if (HttpParam.EN_HTTP_ERRORCODE.EN_HTTP_OK != iRet) { Logger.beginDebug(EspaceService.TAG).p("HttpManager:httpCreateHandle FAIL:" + strRet).end(); httpRequestResult.setStatus(HttpRequestStatus.FAILED); } int handle = StringUtil.findIntElement(strRet, "<handle>", "</handle>", -1); // 是否创建成功 if (0 > handle) { Logger.beginDebug(EspaceService.TAG) .p("HttpManager:httpCreateHandle is ERROR:" + strRet) .end(); httpRequestResult.setStatus(HttpRequestStatus.FAILED); } return handle; }