/** * 上传文件 *  * @param uploadUrl *            上传地址 * @param param *            参数 * @param filepath *            文件路径 * @return 结果 */public static String upload(String uploadUrl, HashMap<String, String> param, String fieldName, String filepath) {StringBuilder result = null;try {String boundary = "---------------------------esa000000000001";URL url = new URL(uploadUrl);HttpURLConnection connection = (HttpURLConnection) url.openConnection();connection.setDoOutput(true);connection.setDoInput(true);connection.setUseCaches(false);connection.setConnectTimeout(1000 * 20);connection.setReadTimeout(1000 * 20);connection.setRequestMethod("POST");connection.setRequestProperty("Connection", "Keep-Alive");connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);connection.setRequestProperty("Charsert", "UTF-8");File file = new File(filepath);StringBuilder sbf = new StringBuilder();sbf.append("--" + boundary + "\r\n");sbf.append("Content-Disposition: form-data; name=\"" + fieldName + "\"; filename=\"" + file.getName() + "\"\r\n");sbf.append("Content-Type: application/octet-stream" + "\r\n\r\n");byte[] fz = sbf.toString().getBytes();StringBuilder sb = new StringBuilder();sb.append("\r\n\r\n");Iterator<String> iterator = param.keySet().iterator();while (iterator.hasNext()) {String key = iterator.next();String value = param.get(key);sb.append("--" + boundary + "\r\n");sb.append("Content-Disposition: form-data; name=\"" + key + "\"\r\n");sb.append("\r\n");sb.append(value + "\r\n");}byte[] before = sb.toString().getBytes();byte[] after = ("\r\n--" + boundary + "--\r\n").getBytes();connection.setRequestProperty("content-length", (before.length + fz.length + file.length() + after.length) + "");DataOutputStream dos = new DataOutputStream(connection.getOutputStream());dos.write(fz);FileInputStream fis = new FileInputStream(file);byte[] buffer = new byte[1024 * 10];int len;while ((len = fis.read(buffer)) != -1) {dos.write(buffer, 0, len);}dos.write(before);dos.write(after);dos.flush();int code = connection.getResponseCode();InputStream stream = connection.getInputStream();byte[] b = new byte[1024 * 10];int l;result = new StringBuilder();while ((l = stream.read(b)) != -1) {result.append(new String(b, 0, l, "utf-8"));}fis.close();dos.close();stream.close();} catch (MalformedURLException e) {} catch (ProtocolException e) {} catch (IOException e) {} finally {}if (result == null) {return null;} else {return result.toString();}}

更多相关文章

  1. Android之canvas详解
  2. Android(安卓)文件的选择
  3. android 上传文件到服务器
  4. Android如何从服务器获取图片
  5. android gps开发必备资料(含测试demo下载)
  6. android 上传文件到服务器
  7. mybatisplus的坑 insert标签insert into select无参数问题的解决
  8. Pycharm安装PyQt5的详细教程
  9. Python技巧匿名函数、回调函数和高阶函数

随机推荐

  1. android多选联系人实现
  2. 有关Material Design新特性的详解。
  3. Power Profiles for Android
  4. Android(安卓)内存溢出解决方案(OOM) 整理
  5. android sqilte3数据库大小的测试
  6. Android使用xml自定义图片
  7. android默认字体大小、高度、宽度
  8. Android我自己的简易(秒表)计时器Chronomet
  9. Android SpannableStringBuilder改变某部
  10. Android webkit log定义