Muhammad Al-Xorazmiy nomidagi Toshkent Axborot Texnologiyalari universiteti
Fan: Tarmoqni dasturlash asoslari
Labaratoriya ishi - 6
Mavzu: Fayllarni ko’chiruvchi tarmoq dasturini yaratish
Bajardi: Sattorov Ulug'bek
Tekshirdi: Berdimuradov Mirzohid
Mavzu: Fayllarni ko‘chiruvchi tarmoq dasturini yaratish
Ishning maqsadi: Talabalarda Java dasturlash tili java.net.* va java.io.* paketlari klasslaridan foydalanib fayllarni ko‘chirish tarmoq dasturini yaratish bo‘yicha amaliy ko‘nikmalar hosil qilish. Nazariy qism: FTP bir xostdan ikkinchisiga fayllar nusxasini ko‘chirish uchun TCP/IP tomonidan ta’minlanadigan bir standart mexanizm hisoblanadi. Bu protokol xostlar o‘rtasida ikki xil aloqa o‘rnatilishini amalga oshiradi: buyruqlar (21-port) va ma’lumotlarni (20-port) uzatish. FTP kliyent asosan 3 ta komponentdan tashkil topadi: - foydalanuvchi interfeysi; - boshqaruv jarayoni; - ma’lumot uzatish jarayoni. FTP server esa 2 ta komponentdan tashkil topadi: - boshqaruv jarayoni; - ma’lumot uzatish jarayoni. Boshqaruv bog‘lanishini yaratish ikki bosqichda amalga oshiriladi: 1. Serverning 21 porti har doim passiv holda ochiq bo‘lib, klient so‘rovini kutib turadi. 2. Klient vaqtinchalik portdan foydalanib, serverdagi 21 port faol holatga keltiradi. Ma’lumot uchun bog‘lanishni yaratish boshqaruv bog‘lanishini yaratishdan farq qilib, 3 bosqichda amalga oshiriladi: 1. Klient vaqtinchalik portdan foydalanib, passiv holatda turadi. 2. Boshqaruv bog‘lanishi orqali serverga port nomeri jo‘natiladi. 3. Server port nomerini qabul qilib, 20 port nomerini faol holatga o‘zgartiradi. 2 FTP klient buyruqlari katta harflarda ASCII formatida bo‘ladi. Buyruqlarni 6 guruhga ajratish mumkin: - Murojaat buyruqlari; - Faylni boshqarish buyruqlari; - Ma’lumotni formatlash buyruqlari; - Portni aniqlash buyruqlari; - Faylni uzatuvchi buyruqlar; - Turli xil buyruqlar. Har bir FTP buyrug‘i eng kamida bitta javobni hosil qiladi va u uch xonali sonni ya’ni xyz hosil qilib, u ham o‘z navbatida ikki qismdan tashkil topadi: sonli qism (kod) va matnli qismi (qo‘shimcha izoh). Birinchi raqam soni 1-5 qaramlaridan biri bo‘lib, buyruq holatini aniqlaydi: - 1yz – harakat boshlandi. Har bir alohida buyruqni qabul qilishidan oldin, alohida javob jo‘natadi; - 2yz – harakat tugatildi. Server boshqa buyruqni qabul qiladi; - 3yz – buyruq qabul qilindi, lekin qo‘shimcha ma’lumot talab etiladi; - 4yz – harakat sodir etilmadi, ammo vaqtinchalik xato; - 5yz – buyruq qabul qilinmadi hamda qayta takrorlanmasligi lozim.
package uz.tatu;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
/**
* A program that demonstrates how to upload files from local computer
* to a remote FTP server using Apache Commons Net API.
* @author www.codejava.net
*/
public class FTPServer {
public static void main(String[] args) {
String server = "www.myserver.com";
int port = 21;
String user = "user";
String pass = "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("D:/Test/Projects.zip");
String firstRemoteFile = "Projects.zip";
InputStream inputStream = new FileInputStream(firstLocalFile);
System.out.println("Start uploading first file");
boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
inputStream.close();
if (done) {
System.out.println("The first file is uploaded successfully.");
}
// APPROACH #2: uploads second file using an OutputStream
File secondLocalFile = new File("C:/Test/Report.doc");
String secondRemoteFile = "test/Report.doc";
inputStream = new FileInputStream(secondLocalFile);
System.out.println("Start uploading second file");
OutputStream outputStream = ftpClient.storeFileStream(secondRemoteFile);
byte[] bytesIn = new byte[4096];
int read = 0;
while ((read = inputStream.read(bytesIn)) != -1) {
outputStream.write(bytesIn, 0, read);
}
inputStream.close();
outputStream.close();
boolean completed = ftpClient.completePendingCommand();
if (completed) {
System.out.println("The second file is uploaded successfully.");
}
} catch (IOException ex) {
System.out.println("Error: " + ex.getMessage());
ex.printStackTrace();
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
Dostları ilə paylaş: |