java发起URL的post表单请求上传视频

javascript 复制代码
     /**
	 * 上传视频
	 */
	@Override
	public R uploadVideo(MultipartFile video) {

			String url = "xxxxxxxxxxx";
			HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
			conn.setConnectTimeout(10*1000);
			conn.setConnectTimeout(5000);
			conn.setReadTimeout(30000);
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setUseCaches(false);
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Connection", "Keep-Alive");
			conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)");
			conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + Boundary);

			DataOutputStream out = new DataOutputStream(conn.getOutputStream());
			//=============================模拟表单提交视频的核心===============top
			StringBuffer strBuf = new StringBuffer();
			strBuf.append("\r\n").append("--").append(Boundary).append("\r\n");
			strBuf.append("Content-Disposition: form-data; name=\"" + "video" + "\"; filename=\"" + video.getOriginalFilename() + "\"\r\n");
			strBuf.append("Content-Type:" + "video/mp4" + "\r\n\r\n");

			out.write(strBuf.toString().getBytes());

			byte[] b = new byte[1024];
			int l = 0;
			out.write(b); // 写入文件
			out.writeUTF("\r\n--"+Boundary+"--\r\n");
			out.flush();
			out.close();
			//=============================模拟表单提交视频的核心===============bottom
			BufferedReader result = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			String s = null;
			StringBuilder r = new StringBuilder();
			while((s=result.readLine())!=null) {
				r.append(s);
			}
			
//			return R.error();
			
		}catch (Exception e){
			return R.error("异常================="+e);
		}
	}

原文链接:https://blog.csdn.net/weixin_43852094/article/details/11535179

相关推荐
码途行者8 小时前
每天10分钟学Go:第17课 接口interface-只规定“能做什么“,不管它是什么
开发语言·后端·golang
头发还在的女程序员8 小时前
Java充电桩平台源码解析:枪口状态、上报时间与页面刷新怎样协同
java·开发语言·充电桩系统·充电桩管理·充电系统·充电小程序
timeDifference₇9 小时前
Python Vibe Coding实操实验报告
开发语言·jvm·python
geovindu10 小时前
python:DecisionTree
开发语言·后端·python·算法
Cosolar10 小时前
从 Java 21 升到 25、Spring Boot 3 升 4:一次可复现的迁移踩坑实录
java·后端·面试
geovindu11 小时前
python: Decision Tree Regressor
开发语言·后端·python·算法·决策树
IT空门:门主12 小时前
LangChain4j 1.24.0 深度解读:新特性、版本对比与学习路线
java·学习
用户37215742613514 小时前
Java 批量操作 Excel 超链接:添加、读取、修改和删除
java
用户37215742613515 小时前
Java 将 Excel 图表导出为图片:指定图表、批量导出与图表工作表
java
炸膛坦客15 小时前
单片机/C/C++八股:(三十七)指针数组 和 数组指针
c语言·开发语言·c++