FileSender.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package com.example.socketdemo.common;
  2. import android.content.Context;
  3. import com.example.socketdemo.base.BaseTransfer;
  4. import com.example.socketdemo.bean.FileInfo;
  5. import com.example.socketdemo.utils.LogUtils;
  6. import java.io.BufferedOutputStream;
  7. import java.io.File;
  8. import java.io.FileInputStream;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.io.OutputStream;
  12. import java.net.Socket;
  13. /**
  14. * Created by AA on 2017/3/24.
  15. */
  16. public class FileSender extends BaseTransfer implements Runnable {
  17. private Context mContext;
  18. /**
  19. * 待发送的文件数据
  20. */
  21. private FileInfo mFileInfo;
  22. /**
  23. * 传送文件的Socket输入输出流
  24. */
  25. private Socket mSocket;
  26. private OutputStream mOutputStream;
  27. /**
  28. * 用来控制线程暂停、恢复
  29. */
  30. private final Object LOCK = new Object();
  31. private boolean mIsPause;
  32. /**
  33. * 该线程是否执行完毕
  34. */
  35. private boolean mIsFinish;
  36. /**
  37. * 设置未执行线程的不执行标识
  38. */
  39. private boolean mIsStop;
  40. /**
  41. * 文件传送监听事件
  42. */
  43. private OnSendListener mOnSendListener;
  44. public FileSender(Context context, Socket socket, FileInfo fileInfo) {
  45. mContext = context;
  46. mSocket = socket;
  47. mFileInfo = fileInfo;
  48. }
  49. /**
  50. * 设置发送监听事件
  51. * @param onSendListener
  52. */
  53. public void setOnSendListener(OnSendListener onSendListener) {
  54. mOnSendListener = onSendListener;
  55. }
  56. @Override
  57. public void run() {
  58. if(mIsStop) {
  59. return;
  60. }
  61. //初始化
  62. try {
  63. if(mOnSendListener != null) {
  64. mOnSendListener.onStart();
  65. }
  66. init();
  67. } catch (Exception e) {
  68. e.printStackTrace();
  69. LogUtils.i("FileSender init() ------->>> occur expection");
  70. if(mOnSendListener != null) {
  71. mOnSendListener.onFailure(e, mFileInfo);
  72. }
  73. }
  74. //发送文件实体数据
  75. try {
  76. parseBody();
  77. } catch (Exception e) {
  78. e.printStackTrace();
  79. LogUtils.i("FileSender parseBody() ------->>> occur expection");
  80. if(mOnSendListener != null) {
  81. mOnSendListener.onFailure(e, mFileInfo);
  82. }
  83. }
  84. //文件传输完毕
  85. try {
  86. finishTransfer();
  87. } catch (Exception e) {
  88. e.printStackTrace();
  89. LogUtils.i("FileSender finishTransfer() ------->>> occur expection");
  90. if(mOnSendListener != null) {
  91. mOnSendListener.onFailure(e, mFileInfo);
  92. }
  93. }
  94. }
  95. @Override
  96. public void init() throws Exception {
  97. mSocket.setSoTimeout(30 * 1000);
  98. OutputStream os = mSocket.getOutputStream();
  99. mOutputStream = new BufferedOutputStream(os);
  100. }
  101. @Override
  102. public void parseBody() throws Exception {
  103. long fileSize = mFileInfo.getSize();
  104. File file = new File(mFileInfo.getFilePath());
  105. InputStream fis = new FileInputStream(file);
  106. int len = 0;
  107. long total = 0;
  108. byte[] bytes = new byte[BYTE_SIZE_DATA];
  109. long sTime = System.currentTimeMillis();
  110. long eTime = 0;
  111. while ((len = fis.read(bytes)) != -1) {
  112. synchronized (LOCK) {
  113. if(mIsPause) {
  114. try {
  115. LOCK.wait();
  116. } catch (InterruptedException e) {
  117. e.printStackTrace();
  118. }
  119. }
  120. //写入文件
  121. mOutputStream.write(bytes, 0, len);
  122. total += len;
  123. //每隔200毫秒返回一次进度
  124. eTime = System.currentTimeMillis();
  125. if(eTime - sTime > 200) {
  126. sTime = eTime;
  127. if(mOnSendListener != null) {
  128. mOnSendListener.onProgress(total, fileSize);
  129. }
  130. }
  131. }
  132. }
  133. //关闭Socket输入输出流
  134. mOutputStream.flush();
  135. mOutputStream.close();
  136. //文件发送成功
  137. if(mOnSendListener != null) {
  138. mOnSendListener.onSuccess(mFileInfo);
  139. }
  140. mIsFinish = true;
  141. }
  142. @Override
  143. public void finishTransfer() throws Exception {
  144. if(mOutputStream != null) {
  145. try {
  146. mOutputStream.close();
  147. } catch (IOException e) {
  148. }
  149. }
  150. if(mSocket != null && mSocket.isConnected()) {
  151. try {
  152. mSocket.close();
  153. } catch (IOException e) {
  154. e.printStackTrace();
  155. }
  156. }
  157. }
  158. /**
  159. * 暂停发送线程
  160. */
  161. public void pause() {
  162. synchronized (LOCK) {
  163. mIsPause = true;
  164. LOCK.notifyAll();
  165. }
  166. }
  167. /**
  168. * 恢复发送线程
  169. */
  170. public void resume() {
  171. synchronized (LOCK) {
  172. mIsPause = false;
  173. LOCK.notifyAll();
  174. }
  175. }
  176. /**
  177. * 设置当前的发送任务不执行
  178. */
  179. public void stop() {
  180. mIsStop = true;
  181. }
  182. /**
  183. * 文件是否在发送中
  184. * @return
  185. */
  186. public boolean isRunning() {
  187. return !mIsFinish;
  188. }
  189. public interface OnSendListener {
  190. void onStart();
  191. void onProgress(long progress, long total);
  192. void onSuccess(FileInfo fileInfo);
  193. void onFailure(Throwable throwable, FileInfo fileInfo);
  194. }
  195. }