Android文件上传带参数
来源:互联网 发布:c语言编程器 编辑:程序博客网 时间:2024/05/30 23:21
//返回值HttpResult可替换成自己想要的类型.
public static <T> HttpResult<String> post(final String urlStr, final Map<String, String> params, T body) { String BOUNDARY = UUID.randomUUID().toString(); //边界标识 随机生成 String PREFIX = "--", LINE_END = "\r\n"; HttpResult<String> result = new HttpResult<>(); File file = (File) body; HttpURLConnection conn = null; try { URL url = new URL(urlStr); conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(30 * 1000); conn.setConnectTimeout(30 * 1000); conn.setDoInput(true); //允许输入流 conn.setDoOutput(true); //允许输出流 conn.setUseCaches(false); //不允许使用缓存 conn.setRequestMethod("POST"); //请求方式 conn.setRequestProperty("Charset", CHARSET); //设置编码 conn.setRequestProperty("connection", "keep-alive"); conn.setRequestProperty("Content-Type", "multipart/form-data" + ";boundary=" + BOUNDARY); headers.remove("token"); OutputStream out = conn.getOutputStream(); StringBuffer sb = new StringBuffer(); for (Map.Entry<String, String> entry : params.entrySet()) { sb.append(PREFIX).append(BOUNDARY).append(LINE_END) .append("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"" + LINE_END) .append("Content-Type: text/plain; charset=" + CHARSET + LINE_END) .append("Content-Transfer-Encoding: 8bit" + LINE_END) .append(LINE_END).append(entry.getValue()).append(LINE_END); } sb.append(PREFIX).append(BOUNDARY).append(LINE_END) .append("Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getName() + "\"" + LINE_END) .append("Content-Type: application/octet-stream; charset=" + CHARSET + LINE_END) .append(LINE_END); out.write(sb.toString().getBytes(CHARSET)); InputStream is = new FileInputStream(file); byte[] bytes = new byte[1024]; int len; while ((len = is.read(bytes)) != -1) { out.write(bytes, 0, len); } out.write(LINE_END.getBytes()); out.write((PREFIX + BOUNDARY + PREFIX + LINE_END).getBytes()); out.flush(); is.close(); out.close(); int resCode = result.code = conn.getResponseCode(); // 开始发送数据 if (resCode == RES_CODE_SUCCESS) { result.obj = buildString(conn.getInputStream()); } } catch (IOException e) { e.printStackTrace(); result.e = e; } finally { if (conn != null) { conn.disconnect(); } } return result;}
阅读全文
0 0
- Android文件上传带参数
- 带参数文件上传
- ajaxfileupload带参数上传文件
- ajaxfileupload带参数上传文件
- ajaxfileupload带参数上传文件
- okhttp 上传带参数文件
- Android 图片(文件--带参数)批量上传到服务器
- android使用HttpURLConnection实现带参数文件上传
- android使用HttpURLConnection实现带参数文件上传
- android使用HttpURLConnection/HttpClient实现带参数文件上传
- Android带进度条文件上传
- fileupload上传文件时带参数
- springMVC上传多个文件带参数
- PHP文件上传接口(带参数)
- spring mvc上传带参数的文件
- WebUploader文件上传(react),带参数
- Android okhttp带参数上传单张图片
- android带进度的文件上传
- STL容器Set和Multiset
- Pandas学习笔记:pandas基础
- LeetCode-ZigZag Conversion
- android-studio安装过程详解
- 开源地图MapBox自定义(二):基本概念
- Android文件上传带参数
- 重磅 | 周志华最新论文:首个基于决策树集成的自动编码器,表现优于DNN
- 笔记 | 吴恩达Coursera Deep Learning学习笔记
- keep-alive的activated,deactivated钩子,及列表scrollTop值存储,返回时还在该位置
- 资源 | AI、神经网络、机器学习、深度学习以及大数据学习备忘单
- 观点 | 李开复谈AI创业的“风口”和“泡沫”
- 充电 | 打开机器学习的大门,需要了解哪些知识?
- 资源 | 盘点国外NLP领域40大高校及相关知名学者
- 独家 | 我们扒出了这家中国创业公司,竟比苹果iPhone X早两年推出黑科技,还不用借助深度摄像头