baf9df1167001f12a701f1ab1e0b6cf0054ed42a.svn-base 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package com.example.myapplication.utils;
  2. import android.util.Log;
  3. import com.orhanobut.logger.Logger;
  4. import java.io.BufferedReader;
  5. import java.io.DataOutputStream;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.IOException;
  9. import java.io.InputStreamReader;
  10. import java.net.HttpURLConnection;
  11. import java.net.MalformedURLException;
  12. import java.net.URL;
  13. import java.util.UUID;
  14. public class Upload extends Thread {
  15. String url;
  16. String filename;
  17. public String toKen;
  18. String upLoadFileName;
  19. public Upload(String url, String filename, String toten, String netFileName) {
  20. super();
  21. this.url = url;
  22. this.filename = filename;
  23. this.toKen = toten;
  24. this.upLoadFileName = netFileName;
  25. }
  26. /**
  27. * 写表单
  28. */
  29. public void doPostUrl() {
  30. String end = "\r\n";
  31. String prefix = "--";
  32. String boundary = UUID.randomUUID().toString(); // 边界标识 随机生成
  33. try {
  34. DataOutputStream out = null;
  35. FileInputStream fStream = null;
  36. BufferedReader reader = null;
  37. URL mUrl = new URL(url);
  38. try {
  39. HttpURLConnection con = (HttpURLConnection) mUrl
  40. .openConnection();
  41. /* 设定传送的method=POST */
  42. con.setRequestMethod("POST");
  43. /* setRequestProperty */
  44. con.setRequestProperty("Connection", "Keep-Alive");
  45. con.setRequestProperty("Charset", "UTF-8");
  46. con.setRequestProperty("auth-token", toKen);// took
  47. con.setRequestProperty("Content-Type",
  48. "multipart/form-data;boundary=" + boundary);
  49. /* 设定DataOutputStream */
  50. out = new DataOutputStream(
  51. con.getOutputStream());
  52. out.writeBytes(prefix + boundary + end);
  53. out.writeBytes("Content-Disposition: form-data; " + "name="
  54. + upLoadFileName + ";filename=" + "/img.png"
  55. + end);
  56. out.writeBytes(end);
  57. /* 取得文件的FileInputStream */
  58. fStream = new FileInputStream(
  59. new File(filename));
  60. /* 设定每次写入1024bytes */
  61. int bufferSize = 1024;
  62. byte[] buffer = new byte[bufferSize];
  63. int length = -1;
  64. /* 从文件读取数据到缓冲区 */
  65. while ((length = fStream.read(buffer)) != -1) {
  66. /* 将数据写入DataOutputStream中 */
  67. Log.d("111", String.valueOf(length));
  68. out.write(buffer, 0, length);
  69. }
  70. out.writeBytes(end);
  71. out.writeBytes(prefix + boundary + prefix + end);
  72. out.flush();
  73. // int res = con.getResponseCode();
  74. reader = new BufferedReader(
  75. new InputStreamReader(con.getInputStream()));
  76. StringBuffer sb = new StringBuffer();
  77. String str;
  78. while ((str = reader.readLine()) != null) {
  79. sb.append(str);
  80. }
  81. if (mFinishListener != null) {
  82. mFinishListener.onFinish(sb.toString());
  83. }
  84. Logger.d("str: " + sb.toString());
  85. } catch (IOException e) {
  86. if (mFinishListener != null) {
  87. mFinishListener.onFinish(null);
  88. }
  89. } finally {
  90. /* close streams */
  91. try {
  92. if (out != null) {
  93. out.close();
  94. }
  95. if (fStream != null) {
  96. fStream.close();
  97. }
  98. if (reader != null) {
  99. reader.close();
  100. }
  101. } catch (IOException e) {
  102. }
  103. }
  104. } catch (MalformedURLException e) {
  105. if (mFinishListener != null) {
  106. mFinishListener.onFinish(null);
  107. }
  108. }
  109. }
  110. private OnUploadFinishListener mFinishListener;
  111. public void setOnUploadFinishListener(OnUploadFinishListener listener) {
  112. this.mFinishListener = listener;
  113. }
  114. @Override
  115. public void run() {
  116. super.run();
  117. doPostUrl();
  118. }
  119. public interface OnUploadFinishListener {
  120. void onFinish(String message);
  121. }
  122. }