-
Notifications
You must be signed in to change notification settings - Fork 4
/
DownloadUploadHelper.java
344 lines (265 loc) · 10.9 KB
/
DownloadUploadHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package com.ibm.hrl.ms.pz;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.ReentrantLock;
import org.apache.http.util.ByteArrayBuffer;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
public class DownloadUploadHelper {
private static ReentrantLock downloadLock = new ReentrantLock();
private static ReentrantLock updateImageLock = new ReentrantLock();
private static AtomicBoolean updateStarted = new AtomicBoolean();
public void uploadImage(String fileName, String filePath,String cis,String url){
(new UploadFilesTask(fileName, filePath,cis)).execute(url+"/upload.html");
}
public void downloadImage(String url, String downloadedImageName, CameraPreview callback){
(new DownloadFileTask(callback,downloadLock,updateImageLock)).execute(url+"/download.html",downloadedImageName);
}
class UploadFilesTask extends AsyncTask<String, Void,String>{
Throwable error = null;
String fileName;
String filePath;
String cis;
UploadFilesTask(String fileName, String filePath,String cis){
this.filePath = filePath;
this.cis = cis;
this.fileName = fileName;
}
@Override
protected void onPostExecute(String result) {
}
@Override
protected String doInBackground(String... params) {
String url = params[0];
upload(this.fileName,this.filePath,this.cis,url);
return null;
}
}
private byte[] decodeFile(File f){
byte[] byteArray= null;
try {
Log.i("vg", "going to decode file: "+f.getAbsolutePath());
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=16;
Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byteArray = stream.toByteArray();
Log.i("vg", "done decoding file: "+f.getAbsolutePath());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return byteArray;
}
class DownloadFileTask extends AsyncTask<String, Void,String>{
Throwable error = null;
String imagePath;
CameraPreview callback;
String imageName;
Throwable exception;
ReentrantLock downloadLock;
ReentrantLock updateImageLock;
public DownloadFileTask(CameraPreview callback,ReentrantLock downloadLock,ReentrantLock updateImageLock ){
this.callback = callback;
this.downloadLock = downloadLock;
this.updateImageLock = updateImageLock;
}
@Override
protected void onPostExecute(String result) {
try{
updateImageLock.lock();
if (callback != null && result != null && result.length() > 0 && exception == null && !callback.getLocalBackgroundImgName().equals(imageName)){
if (imagePath != null && imagePath.length() > 0 ){
callback.displayBackgroundImage(imageName,imagePath,false,true);
}
}
}finally{
updateStarted.set(false);
updateImageLock.unlock();
}
}
@Override
protected String doInBackground(String... params) {
try{
downloadLock.lock();
if (updateStarted.get()){
return "";
}
updateStarted.set(true);
String url = params[0];
imageName = params[1];
try {
if (callback != null && !callback.getLocalBackgroundImgName().equals(imageName)){
imagePath = download(url,imageName);
}
} catch (IOException e) {
this.exception = e;
}
}catch(Exception e){
e.printStackTrace();
}finally{
downloadLock.unlock();
}
return imagePath;
}
}
private String download(String urlString, String fileName) throws IOException {
FileOutputStream fos = null;
String imagePath=null;
try {
URL url = new URL(urlString+ "?fileName="+fileName); //you can write here any link
long startTime = System.currentTimeMillis();
Log.i("vg", "download begining");
Log.i("vg", "download url:" + url);
Log.i("vg", "downloaded file name:" + fileName);
/* Open a connection to that URL. */
URLConnection ucon = url.openConnection();
/*
* Define InputStreams to read from the URLConnection.
*/
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
/*
* Read bytes to the Buffer until there is nothing more to read(-1).
*/
ByteArrayBuffer baf = new ByteArrayBuffer(5000);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
File albumF = SDHelper.getAlbumDir();
File imageF = File.createTempFile(fileName, SDHelper.JPEG_FILE_SUFFIX, albumF);
/* Convert the Bytes read to a String. */
fos = new FileOutputStream(imageF);
byte[] byteArr = baf.toByteArray();
fos.write(byteArr);
fos.flush();
fos.close();
Log.i("vg", "download ready in" + ((System.currentTimeMillis() - startTime) / 1000) + " sec");
if (byteArr.length > 0){
imagePath = imageF.getAbsolutePath();
Log.i("vg", "download size" + byteArr.length +" bytes");
}else{
Log.w("vg", "an empty image was downloaded to server; image name "+fileName);
}
} catch (IOException e) {
Log.e("DownloadManager", "Error: ",e);
throw e;
}
return imagePath;
}
/*
private void postImageInternal(File data, String fileName, String url) {
try{
HttpClient httpClient = new DefaultHttpClient();
//httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);
HttpPost httpPost = new HttpPost(url);
HttpResponse response = null;
FileEntity tmp = new FileEntity(data,"UTF-8");
httpPost.setEntity(tmp);
try {
response = httpClient.execute(httpPost);
} catch (ClientProtocolException e) {
System.out.println("HTTPHelp : ClientProtocolException : "+e);
} catch (IOException e) {
System.out.println("HTTPHelp : IOException : "+e);
}
System.out.println(response.getStatusLine().toString());
}catch (Exception e){
e.printStackTrace();
}
}*/
private void upload(String fileName,String filePath, String cis, String urlServer){
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
//DataInputStream inputStream = null;
//String pathToOurFile = "/mnt/sdcard/media/audio/notifications/preview.mp3";
//String urlServer = "http://ta-proj02.haifa.ibm.com:9081/RecieverWeb/FileReceiver";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
try
{
Log.i("vg", "upload started: "+fileName);
//FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile) );
File data = new File(filePath);
//FileInputStream fileInputStream = new FileInputStream(data);
try {
cis = URLEncoder.encode(cis,"utf-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
URL url = new URL(urlServer +"?cis="+cis);
connection = (HttpURLConnection) url.openConnection();
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
// Enable POST method
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
outputStream = new DataOutputStream( connection.getOutputStream() );
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + fileName +"\"" + lineEnd);
outputStream.writeBytes(lineEnd);
/*
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
*/
buffer = decodeFile(data);
bufferSize = Math.min(buffer.length, maxBufferSize);
bytesAvailable = buffer.length;
outputStream.write(buffer, 0, buffer.length);
/*
// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
*/
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// Responses from the server (code and message)
int serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
//Toast.makeText(getBaseContext(), serverResponseMessage, Toast.LENGTH_LONG).show();
//fileInputStream.close();
outputStream.flush();
outputStream.close();
Log.i("vg", "upload done: "+fileName+"; response code: "+serverResponseCode+"; response message: "+serverResponseMessage);
}
catch (Exception ex){
Log.e("vg", "error in upload "+fileName+"; msg: "+ex.getMessage(),ex);
//Toast.makeText(getBaseContext(), "exception!"+ex.getMessage(), Toast.LENGTH_LONG).show();
//Exception handling
}
}
}