SHOW:
|
|
- or go back to the newest paste.
1 | - | public class UploadFileTask extends AsyncTask<String, Void, Boolean> |
1 | + | sdfsadfpublic class UploadFileTask extends AsyncTask<String, Void, Boolean> |
2 | { | |
3 | private static final int BUFFER_SIZE = 4096; | |
4 | ||
5 | private String host; | |
6 | private int port; | |
7 | private String username; | |
8 | private String password; | |
9 | ||
10 | private String destDir; | |
11 | private File uploadFile; | |
12 | ||
13 | public UploadFileTask(String host, int port, String username, String password, String destDir, File uploadFile) { | |
14 | this.host = host; | |
15 | this.port = port; | |
16 | this.username = username; | |
17 | this.password = password; | |
18 | this.destDir = destDir; | |
19 | this.uploadFile = uploadFile; | |
20 | } | |
21 | ||
22 | @Override | |
23 | protected Boolean doInBackground(String... params) { | |
24 | ||
25 | FTPUtility util = new FTPUtility(host, port, username, password); | |
26 | ||
27 | try | |
28 | { | |
29 | util.connect(); | |
30 | util.uploadFile(uploadFile, destDir); | |
31 | ||
32 | FileInputStream inputStream = new FileInputStream(uploadFile); | |
33 | byte[] buffer = new byte[BUFFER_SIZE]; | |
34 | int bytesRead = -1; | |
35 | long totalBytesRead = 0; | |
36 | int percentCompleted = 0; | |
37 | long fileSize = uploadFile.length(); | |
38 | ||
39 | while ((bytesRead = inputStream.read(buffer)) != -1) { | |
40 | util.writeFileBytes(buffer, 0, bytesRead); | |
41 | totalBytesRead += bytesRead; | |
42 | percentCompleted = (int) (totalBytesRead * 100 / fileSize); | |
43 | } | |
44 | ||
45 | inputStream.close(); | |
46 | ||
47 | util.finish(); | |
48 | ||
49 | return true; | |
50 | ||
51 | } catch (Exception ex) { | |
52 | Log.e(TAG, "Exception => " + ex); | |
53 | return false; | |
54 | } finally { | |
55 | try { | |
56 | util.disconnect(); | |
57 | } catch (FTPException e) { | |
58 | Log.e(TAG, "FTPException => " + e); | |
59 | } | |
60 | } | |
61 | ||
62 | } | |
63 | ||
64 | protected void onPostExecute(Boolean result) { | |
65 | ||
66 | Log.e(TAG, "File Uploading " + result); | |
67 | }; | |
68 | ||
69 | } |