900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > java|Android仿Form表单以post方式提交文本和文件

java|Android仿Form表单以post方式提交文本和文件

时间:2024-01-07 07:39:13

相关推荐

java|Android仿Form表单以post方式提交文本和文件

独角兽企业重金招聘Python工程师标准>>>

import java.io.ByteArrayOutputStream;import java.io.DataInputStream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import .HttpURLConnection;import .URL;import java.util.HashMap;import java.util.Map;/*** * @作者 XiaoYe yshye@ 仿form表单post提交** @修改日期 12月18日下午2:40:52*/public class YsPostHttpUtil {/*** 每个post参数之间的分隔。随意设定,只要不会和其他的字符串重复即可。*/private static final String BOUNDARY = "----------HV2ymHFg03ehbqgZCaKO6jyH";/*** * @param args* @throws Exception*/public static void main(String[] args) throws Exception {String filepath = "/Users/mac/Downloads/tel.png";String urlStr = "url";Map<String, String> textMap = new HashMap<String, String>();textMap.put("accountcode", "wy");textMap.put("billid", "00E585FE1F0066C6AD5A");Map<String, String> fileMap = new HashMap<String, String>();fileMap.put("userfile", filepath);String ret = formUpload(urlStr, textMap, fileMap);System.out.println(ret);}/*** 提交表单* * @param urlStr* @param textMap* @param fileMap* @return* @throws Exception*/public static String formUpload(String urlStr, Map<String, String> textMap, Map<String, String> fileMap)throws Exception {// 头String boundary = BOUNDARY;// 传输内容StringBuffer contentBody = new StringBuffer("--" + BOUNDARY);// 尾String endBoundary = "\r\n--" + boundary + "--\r\n";// 2 向服务器发送post请求URL url = new URL(urlStr);HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 发送POST请求必须设置如下两行connection.setDoOutput(true);connection.setDoInput(true);connection.setUseCaches(false);connection.setRequestMethod("POST");connection.setRequestProperty("Connection", "Keep-Alive");connection.setRequestProperty("Charset", "UTF-8");connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);OutputStream out = connection.getOutputStream();// 2 写入内容// 2.1 处理普通表单域(即形如key = value对)的POST请求for (String key : textMap.keySet()) {contentBody.append("\r\n").append("Content-Disposition: form-data; name=\"").append(key + "\"").append("\r\n").append("\r\n").append(textMap.get(key)).append("\r\n").append("--").append(boundary);}out.write(contentBody.toString().getBytes("UTF-8"));// 2.2 处理文件上传for (String key : fileMap.keySet()) {contentBody = new StringBuffer();contentBody.append("\r\n").append("Content-Disposition:form-data; name=\"").append(key + "\"; ")// form中field的名称.append("filename=\"").append(fileMap.get(key) + "\"")// 上传文件的文件名,包括目录.append("\r\n").append("Content-Type:multipart/form-data").append("\r\n\r\n");out.write(contentBody.toString().getBytes("utf-8"));// 开始真正向服务器写文件File file = new File(fileMap.get(key));DataInputStream dis = new DataInputStream(new FileInputStream(file));int bytes = 0;byte[] bufferOut = new byte[(int) file.length()];bytes = dis.read(bufferOut);out.write(bufferOut, 0, bytes);dis.close();contentBody.append(boundary);out.write(contentBody.toString().getBytes("utf-8"));}out.write((boundary+"--\r\n").getBytes("UTF-8"));// 3. 写结尾out.write(endBoundary.getBytes("utf-8"));out.flush();out.close();// 4. 获得服务器的响应结果和状态码int responseCode = connection.getResponseCode();if (responseCode == 200) {return changeInputStream(connection.getInputStream(), "utf-8");} else {return "";}}/*** 获得网络返回值* * @param inputStream* @param encode* @return*/public static String changeInputStream(InputStream inputStream, String encode) {ByteArrayOutputStream outputStream = new ByteArrayOutputStream();byte[] data = new byte[1024];int len = 0;String result = "";if (null != inputStream) {try {while ((len = inputStream.read(data)) != -1) {outputStream.write(data, 0, len);}result = new String(outputStream.toByteArray(), encode);// System.out.println(result);} catch (IOException e) {return "";//e.printStackTrace();}}return result;}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。