Android文件上传

1/** 2 * 上传文件 3 * 4 * @param uploadUrl 5 * 上传地址 6 * @param param 7 * 参数 8 * @param filepath 9 * 文件路径 10 * @return 结果 11 */ 12 public static String upload(String uploadUrl, HashMap<String, String> param, String fieldName, String filepath) { 13 StringBuilder result = null; 14 try { 15 String boundary = "---------------------------esa000000000001"; 16 URL url = new URL(uploadUrl); 17 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 18 connection.setDoOutput(true); 19 connection.setDoInput(true); 20 connection.setUseCaches(false); 21 connection.setConnectTimeout(1000 * 20); 22 connection.setReadTimeout(1000 * 20); 23 connection.setRequestMethod("POST"); 24 connection.setRequestProperty("Connection", "Keep-Alive"); 25 connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); 26 connection.setRequestProperty("Charsert", "UTF-8"); 27 28 File file = new File(filepath); 29 StringBuilder sbf = new StringBuilder(); 30 sbf.append("--" + boundary + "\r\n"); 31 sbf.append("Content-Disposition: form-data; name=\"" + fieldName + "\"; filename=\"" + file.getName() + "\"\r\n"); 32 sbf.append("Content-Type: application/octet-stream" + "\r\n\r\n"); 33 byte[] fz = sbf.toString().getBytes(); 34 StringBuilder sb = new StringBuilder(); 35 sb.append("\r\n\r\n"); 36 Iterator<String> iterator = param.keySet().iterator(); 37 while (iterator.hasNext()) { 38 String key = iterator.next(); 39 String value = param.get(key); 40 sb.append("--" + boundary + "\r\n"); 41 sb.append("Content-Disposition: form-data; name=\"" + key + "\"\r\n"); 42 sb.append("\r\n"); 43 sb.append(value + "\r\n"); 44 } 45 byte[] before = sb.toString().getBytes(); 46 byte[] after = ("\r\n--" + boundary + "--\r\n").getBytes(); 47 connection.setRequestProperty("content-length", (before.length + fz.length + file.length() + after.length) + ""); 48 DataOutputStream dos = new DataOutputStream(connection.getOutputStream()); 49 dos.write(fz); 50 FileInputStream fis = new FileInputStream(file); 51 byte[] buffer = new byte[1024 * 10]; 52 int len; 53 while ((len = fis.read(buffer)) != -1) { 54 dos.write(buffer, 0, len); 55 } 56 dos.write(before); 57 dos.write(after); 58 dos.flush(); 59 int code = connection.getResponseCode(); 60 InputStream stream = connection.getInputStream(); 61 byte[] b = new byte[1024 * 10]; 62 int l; 63 result = new StringBuilder(); 64 while ((l = stream.read(b)) != -1) { 65 result.append(new String(b, 0, l, "utf-8")); 66 } 67 fis.close(); 68 dos.close(); 69 stream.close(); 70 } catch (MalformedURLException e) { 71 } catch (ProtocolException e) { 72 } catch (IOException e) { 73 } finally { 74 75 } 76 if (result == null) { 77 return null; 78 } else { 79 return result.toString(); 80 } 81 }
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

Android文件上传 - HelloWorld