5G消息 MESSAGE OVER 5G-多媒体文件下载:代码样例

时间:2024-04-28 11:16:18

代码样例

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());
}
}
support.huaweicloud.com/devg-messageover5g/messageover5g_04_0002_2.html