博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Multipart HTTP Requests
阅读量:6453 次
发布时间:2019-06-23

本文共 2137 字,大约阅读时间需要 7 分钟。

hot3.png

public String multipartRequest(String urlTo, String post, String filepath, String filefield) throws ParseException, IOException {	HttpURLConnection connection = null;	DataOutputStream outputStream = null;	InputStream inputStream = null;		String twoHyphens = "--";	String boundary =  "*****"+Long.toString(System.currentTimeMillis())+"*****";	String lineEnd = "\r\n";		String result = "";		int bytesRead, bytesAvailable, bufferSize;	byte[] buffer;	int maxBufferSize = 1*1024*1024;		String[] q = filepath.split("/");	int idx = q.length - 1;		try {		File file = new File(filepath);		FileInputStream fileInputStream = new FileInputStream(file);				URL url = new URL(urlTo);		connection = (HttpURLConnection) url.openConnection();				connection.setDoInput(true);		connection.setDoOutput(true);		connection.setUseCaches(false);				connection.setRequestMethod("POST");		connection.setRequestProperty("Connection", "Keep-Alive");		connection.setRequestProperty("User-Agent", "Android Multipart HTTP Client 1.0");		connection.setRequestProperty("Content-Type", "multipart/form-data; boundary="+boundary);				outputStream = new DataOutputStream(connection.getOutputStream());		outputStream.writeBytes(twoHyphens + boundary + lineEnd);		outputStream.writeBytes("Content-Disposition: form-data; name=\"" + filefield + "\"; filename=\"" + q[idx] +"\"" + lineEnd);		outputStream.writeBytes("Content-Type: image/jpeg" + lineEnd);		outputStream.writeBytes("Content-Transfer-Encoding: binary" + lineEnd);		outputStream.writeBytes(lineEnd);				bytesAvailable = fileInputStream.available();		bufferSize = Math.min(bytesAvailable, maxBufferSize);		buffer = new byte[bufferSize];				bytesRead = fileInputStream.read(buffer, 0, bufferSize);		while(bytesRead > 0) {			outputStream.write(buffer, 0, bufferSize);			bytesAvailable = fileInputStream.available();			bufferSize = Math.min(bytesAvailable, maxBufferSize);			bytesRead = fileInputStream.read(buffer, 0, bufferSize);		}				outputStream.writeBytes(lineEnd);				// Upload POST Data		String[] posts = post.split("&");		int max = posts.length;		for(int i=0; i

转载于:https://my.oschina.net/oppo4545/blog/195226

你可能感兴趣的文章
JMeter—断言
查看>>
C++的新类创建:继承与组合
查看>>
m5-第9周作业
查看>>
odoo 权限设置
查看>>
asp操作access提示“无法从指定的数据表中删除”
查看>>
git bash 风格调整
查看>>
997D Cycles in product
查看>>
bzoj4589 Hard Nim
查看>>
java实现pdf旋转_基于Java实现PDF文本旋转倾斜
查看>>
java二维数组内存模型_C++二级指针第二种内存模型(二维数组)
查看>>
java static import 与 import_Java中的import和static import语句之间有什么区别?
查看>>
python time库3.8_python3中datetime库,time库以及pandas中的时间函数区别与详解
查看>>
java 代替Python_Java总是“沉沉浮浮”,替代者会是Python?
查看>>
贪吃蛇java程序简化版_JAVA简版贪吃蛇
查看>>
poi java web_WebPOI JavaWeb 项目 导出excel表格(.xls) Develop 238万源代码下载- www.pudn.com...
查看>>
java 顶点着色_金属顶点着色器绘制纹理点
查看>>
php扩展有哪些G11,php 几个扩展(extension)的安装笔记
查看>>
ajax长连接 php,ajax怎么实现服务器与浏览器长连接
查看>>
oracle报1405,【案例】Oracle报错ORA-15054 asm diskgroup无法mount的解决办法
查看>>
php 5.4.24 win32,PHP 5.4.14 和 PHP 5.3.24 发布
查看>>