1
+ package com .googleresearch .capturesync ;
2
+
3
+
4
+ import java .io .DataInputStream ;
5
+ import java .io .DataOutputStream ;
6
+ import java .io .File ;
7
+ import java .io .FileInputStream ;
8
+ import java .io .InputStream ;
9
+ import java .io .OutputStream ;
10
+ import java .net .HttpURLConnection ;
11
+ import java .net .URL ;
12
+ import android .os .AsyncTask ;
13
+ import android .util .Log ;
14
+
15
+ public class RemoteFileUpload extends AsyncTask <String , Integer , Boolean > {
16
+
17
+ @ Override
18
+ protected Boolean doInBackground (String ... selectedFilePaths ) {
19
+ HttpURLConnection conn = null ;
20
+ DataOutputStream os = null ;
21
+ DataInputStream inputStream = null ;
22
+
23
+ String urlServer = "http://192.168.5.1:5000/upload" ;
24
+
25
+ String lineEnd = "\r \n " ;
26
+ String twoHyphens = "--" ;
27
+ String boundary = "*****" ;
28
+ int bytesRead , bytesAvailable , bufferSize , bytesUploaded = 0 ;
29
+ byte [] buffer ;
30
+ int maxBufferSize = 2 * 1024 * 1024 ;
31
+
32
+ String uploadname = "test_file" ;
33
+
34
+ try {
35
+ FileInputStream fis = new FileInputStream (new File (selectedFilePaths [0 ]));
36
+
37
+ URL url = new URL (urlServer );
38
+ conn = (HttpURLConnection ) url .openConnection ();
39
+ conn .setChunkedStreamingMode (maxBufferSize );
40
+
41
+ // POST settings.
42
+ conn .setDoInput (true );
43
+ conn .setDoOutput (true );
44
+ conn .setUseCaches (false );
45
+ conn .setRequestMethod ("POST" );
46
+ conn .setRequestProperty ("Connection" , "Keep-Alive" );
47
+ conn .setRequestProperty ("Content-Type" , "multipart/form-data; boundary=" + boundary );
48
+ conn .setRequestProperty ("Name" , "Testing request" );
49
+ conn .connect ();
50
+
51
+ os = new DataOutputStream (conn .getOutputStream ());
52
+ os .writeBytes (twoHyphens + boundary + lineEnd );
53
+ os .writeBytes ("Content-Disposition: form-data; name=\" uploadedfile\" ;filename=\" " + uploadname + "\" " + lineEnd );
54
+ os .writeBytes (lineEnd );
55
+
56
+ bytesAvailable = fis .available ();
57
+ System .out .println ("available: " + String .valueOf (bytesAvailable ));
58
+ bufferSize = Math .min (bytesAvailable , maxBufferSize );
59
+ buffer = new byte [bufferSize ];
60
+
61
+ bytesRead = fis .read (buffer , 0 , bufferSize );
62
+ bytesUploaded += bytesRead ;
63
+ while (bytesRead > 0 ) {
64
+ os .write (buffer , 0 , bufferSize );
65
+ bytesAvailable = fis .available ();
66
+ bufferSize = Math .min (bytesAvailable , maxBufferSize );
67
+ buffer = new byte [bufferSize ];
68
+ bytesRead = fis .read (buffer , 0 , bufferSize );
69
+ bytesUploaded += bytesRead ;
70
+ }
71
+ System .out .println ("uploaded: " + String .valueOf (bytesUploaded ));
72
+ os .writeBytes (lineEnd );
73
+ os .writeBytes (twoHyphens + boundary + twoHyphens + lineEnd );
74
+
75
+ // Responses from the server (code and message)
76
+ conn .setConnectTimeout (2000 ); // allow 2 seconds timeout.
77
+ int rcode = conn .getResponseCode ();
78
+ if (rcode == 200 ){
79
+ System .out .println ("Success" );
80
+ }
81
+ fis .close ();
82
+ os .flush ();
83
+ os .close ();
84
+
85
+ } catch (Exception ex ) {
86
+ ex .printStackTrace ();
87
+ //return false;
88
+ }
89
+ return null ;
90
+ }
91
+
92
+ @ Override
93
+ protected void onProgressUpdate (Integer ... values ) {
94
+ Log .i ("PROGRESSO" , values [0 ]+"" );
95
+
96
+ //super.onProgressUpdate(values);
97
+ }
98
+ @ Override
99
+ protected void onPostExecute (Boolean result ) {
100
+ super .onPostExecute (result );
101
+ }
102
+
103
+ }
0 commit comments