88import java .io .OutputStream ;
99import java .util .ArrayList ;
1010import java .util .Random ;
11+ import java .util .concurrent .*;
1112
1213public class MultipartEntity extends AbstractHttpEntity {
1314 private String mBoundary ;
@@ -33,7 +34,7 @@ public void addFile(String field, String contentType, String fileName, InputStre
3334
3435 @ Override
3536 public boolean isRepeatable () {
36- return false ;
37+ return true ;
3738 }
3839
3940 @ Override
@@ -55,6 +56,7 @@ public InputStream getContent() throws IOException, IllegalStateException {
5556
5657 @ Override
5758 public void writeTo (OutputStream outputStream ) throws IOException {
59+ writed = 0 ;
5860 outputStream .write (mData .toString ().getBytes ());
5961 outputStream .flush ();
6062 writed += mData .toString ().getBytes ().length ;
@@ -80,6 +82,7 @@ public boolean isStreaming() {
8082 public void setProcessNotify (IOnProcess ret ) {
8183 mNotify = ret ;
8284 }
85+ ExecutorService executor = Executors .newFixedThreadPool (1 );
8386
8487 class FileInfo {
8588
@@ -112,12 +115,18 @@ public void writeTo(OutputStream outputStream) throws IOException {
112115
113116 int blockSize = (int ) (getContentLength () / 100 );
114117 if (blockSize > 256 * 1024 ) blockSize = 256 * 1024 ;
115- if (blockSize < 16 * 1024 ) blockSize = 16 * 1024 ;
118+ if (blockSize < 32 * 1024 ) blockSize = 32 * 1024 ;
116119 long index = 0 ;
117120 long length = mIsa .length ();
118121 while (index < length ) {
119122 int readLength = (int ) StrictMath .min ((long ) blockSize , mIsa .length () - index );
120- outputStream .write (mIsa .read (index , readLength ));
123+ int timeout = readLength * 2 ;
124+ try {
125+ write (timeout , outputStream , mIsa .read (index , readLength ));
126+ } catch (Exception e ) {
127+ mNotify .onFailure (e );
128+ return ;
129+ }
121130 index += blockSize ;
122131 outputStream .flush ();
123132 writed += readLength ;
@@ -130,6 +139,18 @@ public void writeTo(OutputStream outputStream) throws IOException {
130139 }
131140 }
132141
142+ private void write (int timeout , final OutputStream outputStream , final byte [] data ) throws InterruptedException , ExecutionException , TimeoutException {
143+ Callable <Object > readTask = new Callable <Object >() {
144+ @ Override
145+ public Object call () throws Exception {
146+ outputStream .write (data );
147+ return null ;
148+ }
149+ };
150+ Future <Object > future = executor .submit (readTask );
151+ future .get (timeout , TimeUnit .MILLISECONDS );
152+ }
153+
133154 private static String getRandomString (int length ) {
134155 String base = "abcdefghijklmnopqrstuvwxyz0123456789" ;
135156 Random random = new Random ();
0 commit comments