更新时间:2024-11-21 GMT+08:00
多媒体文件下载
代码样例
import static java.nio.charset.StandardCharsets.UTF_8; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.logging.Level; import java.util.logging.Logger; /** * 功能描述 */ public class FileDownload { /** * 正确码 * */ public static final int SUCCESS_VALUE = 200; /** * 错误码 * */ public static final int ERR_VALUE = 204; private FileDownload() { } /** * 文件下载功能 * * @param args * @throws IOException * */ public static void main(String[] args) throws IOException { Logger logger = Logger.getLogger(FileDownload.class.getName()); logger.setLevel(Level.WARNING); InputStream is; String url = "http://127.0.0.1:8323/openchatbot/v2/" + "sip:chatbotid32test1@botplatform.rcs.chinamobile.com/files"; URL realUrl = new URL(url); HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection(); // 设置请求方式 connection.setRequestMethod("GET"); connection.setRequestProperty("Authorization", "Username=\"appId32test1\",Password=\"******\""); connection.setRequestProperty("X-WSSE", "UsernameToken Username=\"appId32test1\""); connection.setRequestProperty("File-Location", "http://10.134.204.203:8087/Content111"); connection.connect(); int status = connection.getResponseCode(); if (status != SUCCESS_VALUE) { if (status == ERR_VALUE) { logger.warning("No Content,sucess,nobody。"); is = connection.getInputStream(); } else { // 400/401 logger.warning("err"); is = connection.getErrorStream(); } } else { // 200 logger.warning("OK,DELETE"); is = connection.getInputStream(); } BufferedReader br; br = new BufferedReader(new InputStreamReader(is, UTF_8)); String line; StringBuilder result = new StringBuilder(); // 读取数据 while ((line = br.readLine()) != null) { result.append(line); } // 关流操作,这个很重要 is.close(); br.close(); // 关闭连接 connection.disconnect(); logger.warning("mabye is null" + result.toString()); } }
参数名称 |
参数说明 |
---|---|
url |
请求地址 |
Connection |
发起请求时,建立连接的认证信息 |
消息样例
GET status:200
OK,DELETE请求成功,响应消息有消息体。
响应消息(可能消息体存在但是为空)。
父主题: JAVA