更新时间:2024-11-21 GMT+08:00
多媒体文件上传
代码样例
import static java.nio.charset.StandardCharsets.UTF_8; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.HttpURLConnection; import java.net.URL; import java.security.NoSuchAlgorithmException; import java.util.HashMap; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; /** * 5G媒体文件上传代码样例 */ public final class FileUpload { static final int SUCCESS = 200; static final int ERR = 204; private FileUpload() { } /** * 功能描述 * * @param args * @throws IOException * @throws NoSuchAlgorithmException */ public static void main(String[] args) throws IOException, NoSuchAlgorithmException { File file = new File("src/1.txt"); file.setFileId("12341234"); file.setFileSize(20); file.setFileUrl("src/1.txt"); file.setStatus("OK"); file.setValidity(""); Map<String, Object> map = new HashMap<>(); map.put("File",file); post("http://10.120.207.128:8323/openchatbot/v2" + "/sip:chatbotid32test1@botplatform.rcs.chinamobile.com/files",map); } /** * 定义post方法 * * @param url * @param params * @Parameters: parms,访问参数,Map形式,以K-v存放多组值。 * @return null * @throws IOException * @throws NoSuchAlgorithmException */ public static String post(String url, Map<String, Object> params) throws IOException, NoSuchAlgorithmException { Logger logger = Logger.getLogger(FileUpload.class.getName()); logger.setLevel(Level.WARNING); logger.warning("url" + url); // 获取认证信息authorization参数信息 String authorization = "Username=\"appId32test1\",Password=\"******\""; // 获取认证信息X-WSSE String xwsse = "UsernameToken Username=\"appId32test1\""; URL url1 = new URL(url); InputStream is; // 将url以open方法返回的urlConnection连接强转为HttpURLConnection连接(标识一个url所引用的远程对象连接),此时cnnection只是为一个连接对象,待连接中 HttpURLConnection conn = (HttpURLConnection) url1.openConnection(); // 设置连接输出流为true,默认false (post请求是以流的方式隐式的传递参数) conn.setDoOutput(true); // 设置连接输入流为true conn.setDoInput(true); // 设置请求方式为post conn.setRequestMethod("POST"); // post请求缓存设为false conn.setUseCaches(false); // 设置该HttpURLConnection实例是否自动执行重定向 conn.setInstanceFollowRedirects(true); // 设置内容的类型,设置为经过urlEncoded编码过的from参数 conn.setRequestProperty("Content-Type", "application/json"); conn.setRequestProperty("Authorization", authorization); conn.setRequestProperty("X-WSSE", xwsse); conn.setRequestProperty("User-Agent", "2"); ObjectMapper objectMapper = new ObjectMapper(); PrintWriter pw = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), UTF_8)); pw.write(objectMapper.writeValueAsString(params)); // 查看传参信息 logger.warning(objectMapper.writeValueAsString(params)); pw.flush(); pw.close(); // 建立连接(请求未开始,直到connection.getInputStream()方法调用时才发起,以上各个参数设置需在此方法之前进行) conn.connect(); // 输出连接信息,实际使用时去掉 outConnInfo(conn, url1); // 连接发起请求,处理服务器响应 (从连接获取到输入流并包装为bufferedReader) int status = conn.getResponseCode(); logger.warning("status:" + status); if (status != SUCCESS) { logger.warning("No Content"); if (status == ERR) { logger.warning("err"); is = conn.getInputStream(); } else { // 400/401 is = conn.getErrorStream(); } } else { // 200 logger.warning("OK,SUCESS"); is = conn.getInputStream(); } BufferedReader br = new BufferedReader(new InputStreamReader(is, UTF_8)); String line; StringBuilder resultMsg = new StringBuilder(); while ((line = br.readLine()) != null) { // 读取数据 resultMsg.append(line); } // 关流,这个很重要 is.close(); br.close(); // 关闭连接 conn.disconnect(); logger.warning("resultMsg.toString()" + resultMsg.toString()); return null; } // 定义传入的文件,携带需要上传的参数 /** * 功能描述 * */ public static class File { private String fileId; private Integer fileSize; private String status; private String fileUrl; private String validity; /** * 功能描述 * * @param s */ public File(String s) { } public String getFileId() { return fileId; } public void setFileId(String fileId) { this.fileId = fileId; } public Integer getFileSize() { return fileSize; } public void setFileSize(Integer fileSize) { this.fileSize = fileSize; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public String getFileUrl() { return fileUrl; } public void setFileUrl(String fileUrl) { this.fileUrl = fileUrl; } public String getValidity() { return validity; } public void setValidity(String validity) { this.validity = validity; } } /** * 输出连接信息 ,实际业务不涉及 * */ private static void outConnInfo(HttpURLConnection conn, URL url) throws IOException { Logger logger = Logger.getLogger(FileUpload.class.getName()); logger.setLevel(Level.WARNING); logger.warning("conn.getResponseCode():" + conn.getResponseCode()); logger.warning("conn.getURL():" + conn.getURL()); logger.warning("conn.getRequestMethod():" + conn.getRequestMethod()); logger.warning("conn.getContentType():" + conn.getContentType()); logger.warning("conn.getReadTimeout():" + conn.getReadTimeout()); logger.warning("conn.getResponseMessage():" + conn.getResponseMessage()); logger.warning("url.getDefaultPort():" + url.getDefaultPort()); logger.warning("url.getFile():" + url.getFile()); logger.warning("url.getHost():" + url.getHost()); logger.warning("url.getPath():" + url.getPath()); logger.warning("url.getPort():" + url.getPort()); logger.warning("url.getProtocol():" + url.getProtocol()); } }
参数名称 |
参数说明 |
---|---|
File |
创建上传的内容(文件) |
Map |
封装发送请求时,请求的内容 |
Connection |
发起请求时要携带的请求认证信息 |
消息样例
这个是传入的请求 urlhttp://10.120.207.128:8323/openchatbot/v2/sip:chatbotid32test1@botplatform.rcs.chinamobile.com/files 以下是请求参数信息--- {"File":{"fileId":"12341234","fileSize":20,"status":"OK","fileUrl":"src/1.txt","validity":""}} 以上是请求参数信息--- 以下是获取响应信息=== conn.getResponseCode():200 conn.getURL():http://10.120.207.128:8323/openchatbot/v2/sip:chatbotid32test1@botplatform.rcs.chinamobile.com/files conn.getRequestMethod():POST conn.getContentType():application/xml conn.getReadTimeout():0 conn.getResponseMessage():OK url.getDefaultPort():80 url.getFile():/openchatbot/v2/sip:chatbotid32test1@botplatform.rcs.chinamobile.com/files url.getHost():10.120.207.128 url.getPath():/openchatbot/v2/sip:chatbotid32test1@botplatform.rcs.chinamobile.com/files url.getPort():8323 url.getProtocol():http url.getQuery():null url.getRef():null url.getUserInfo():null 以上是获取响应信息===DDD= status:200
父主题: JAVA