Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void FTPDownloadto() {
- String server = "192.168.0.103";
- int port = 21;
- String user = "anonymous";
- String pass = "";
- FTPClient ftpClient = new FTPClient();
- try {
- ftpClient.connect(server, port);
- ftpClient.login(user, pass);
- ftpClient.enterLocalPassiveMode();
- ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
- // APPROACH #1: uploads first file using an InputStream
- File firstLocalFile = new File("\storage\ext_sd\DCIM\100MEDIA\IMAG0171.jpg");
- String firstRemoteFile = "IMAG0171.jpg";
- InputStream inputStream = new FileInputStream(firstLocalFile);
- Toast.makeText(MainActivity.this, "Начало загрузки первого файла", Toast.LENGTH_SHORT).show();
- boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
- inputStream.close();
- if (done) {
- Toast.makeText(MainActivity.this, "Первый фаил загружен успешно", Toast.LENGTH_SHORT).show();
- }
- inputStream.close();
- boolean completed = ftpClient.completePendingCommand();
- if (completed) {
- Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT).show();
- }
- } catch (IOException ex) {
- Toast.makeText(MainActivity.this, "Ошибка:" + ex.getMessage(), Toast.LENGTH_SHORT).show();
- ex.printStackTrace();
- } finally {
- try {
- if (ftpClient.isConnected()) {
- ftpClient.logout();
- ftpClient.disconnect();
- }
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- }
- android.os.NetworkOnMain.ThreadExseotion
- <uses-permission android:name="android.permission.INTERNET" />
- class FTPUploadFile extends AsyncTask<Void, Integer, Void> {
- //прежде чем задача выполнена
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- Toast.makeText(MainActivity.this, "Начало процесса", Toast.LENGTH_SHORT).show();
- }
- //после окончания задачи
- @Override
- protected void onPostExecute(Void aVoid) {
- super.onPostExecute(aVoid);
- Toast.makeText(MainActivity.this, "Процесс окончен", Toast.LENGTH_SHORT).show();
- }
- @Override
- protected void onProgressUpdate(Integer... values) {
- }
- //задача
- @Override
- protected Void doInBackground(Void... params) {
- String server = "192.168.0.103";
- int port = 21;
- String user = "anonymous";
- String pass = "";
- FTPClient ftpClient = new FTPClient();
- try {
- ftpClient.connect(server, port);
- ftpClient.login(user, pass);
- ftpClient.enterLocalPassiveMode();
- ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
- // APPROACH #1: uploads first file using an InputStream
- File firstLocalFile = new File("\storage\ext_sd\DCIM\100MEDIA\IMAG0171.jpg");
- String firstRemoteFile = "IMAG0171.jpg";
- InputStream inputStream = new FileInputStream(firstLocalFile);
- Toast.makeText(MainActivity.this, "Начало загрузки первого файла", Toast.LENGTH_SHORT).show();
- boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
- inputStream.close();
- if (done) {
- Toast.makeText(MainActivity.this, "Первый фаил загружен успешно", Toast.LENGTH_SHORT).show();
- }
- inputStream.close();
- boolean completed = ftpClient.completePendingCommand();
- if (completed) {
- Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT).show();
- }
- } catch (IOException ex) {
- Toast.makeText(MainActivity.this, "Ошибка:" + ex.getMessage(), Toast.LENGTH_SHORT).show();
- ex.printStackTrace();
- } finally {
- try {
- if (ftpClient.isConnected()) {
- ftpClient.logout();
- ftpClient.disconnect();
- }
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- return null;
- }
Add Comment
Please, Sign In to add comment