2023年12月29日发(作者:)
QT程序FTP服务器
12级电子新能源本2班
2
张恒
一、 QT简介
QT是一个跨平台的C++图形化用户界面的应用程序开发框架。它既可以开发GUI程序,也用于开发非GUI程序,比如控制台工具和服务器。在QT Creator 3.1.0正式发布后,QT实现了对IOS、Android、WP的全面支持。
二、 FTP服务器
FTP是文件传输协议的英文简称,用于Internet上的控制文件的双向传输。同多数Internet服务器一样,FTP也是分为两部分,即客户端程序与服务器系统。本次所做出的图形化界面程序为FTP的客户端程序。
三、 程序介绍
1. UI介绍
FTP客户端程序的UI界面是图所示,在UI中主要由连接、上传、下载、退出、返回、FTP地址、用户名、密码等组成,并且在下方留出大窗口用于显示FTP服务器中的文件。
2. 程序源代码介绍
3. #include "ftp.h"
4. #include "ui_ftp.h"
5.
6. Ftp::Ftp(QWidget *parent)
7. : QWidget(parent), ui(new Ui::Ftp), ftpobj(0)
8. {
9. ui->setupUi(this);
10. //ui->lineEdit->setText("");
11. //ui->comboBox->addItem("192.168.1.253");
12. ui->lineEdit_3->setEchoMode(QLineEdit::Password);
13. //ui->treeWidget->setAcceptDrops(true);
14. this->setAcceptDrops(true);
15. //model = new QDirModel;
16. //ui->treeView->setModel(model);
17. //ui->treeView->setAcceptDrops(true);
18.
19. ui->label_2->setText(tr("Please enter the name of an FTP server."));
20. ui->lineEdit_2->setText("");
21. ui->lineEdit_3->setText("");
22.
23. ui->treeWidget->setEnabled(false);
24. ui->treeWidget->setRootIsDecorated(false);
25. //ui->treeWidget->setHeaderLabels(QStringList() <
<
26. ui->treeWidget->header()->setStretchLastSection(false);
27.
28. ui->pushButton_2->setDefault(true);
29. ui->pushButton->setEnabled(false);
30. ui->radioButton_2->setChecked(true);
31. ui->pushButton_3->setEnabled(false);
32. progressDialog = new QProgressDialog(this);
33.
34. connect(ui->checkBox, SIGNAL(clicked()), this, SLOT(enableCheckBox()));
35. connect(ui->treeWidget, SIGNAL(itemActivated(QTreeWidgetItem *, int)),
36. this, SLOT(processItem(QTreeWidgetItem *, int)));
37. connect(ui->treeWidget, SIGNAL(currentItemChanged(QTreeWidgetItem *,
QTreeWidgetItem *)),
38. this, SLOT(enableDownloadButton()));
39. connect(progressDialog, SIGNAL(canceled()), this, SLOT(cancelDownload()));
40. connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(connectOrDisconnect()));
41. connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(cdToParent()));
42. connect(ui->pushButton_3, SIGNAL(clicked()), this, SLOT(downloadFile()));
43. connect(ui->pushButton_4, SIGNAL(clicked()), this, SLOT(close()));
44. connect(ui->pushButton_5, SIGNAL(clicked()), this, SLOT(uploadFile()));
45. //红色代码用于设置信号槽,定义了PushButton、PushButton_2、PushButton_3等按钮的使用
46. setWindowTitle(tr("FTP"));
47. }
48.
49. Ftp::~Ftp()
50. {
51. delete ui;
52. }
53.
54.
55. QSize Ftp::sizeHint() const
56. {
57. return QSize(500, 300);
58. }
59.
60. //![0]
61. void Ftp::connectOrDisconnect()
62. {
63. if(!ui->checkBox->isChecked()){
64. if(ui->lineEdit_2->text().isEmpty()||ui->lineEdit_3->text().isEmpty())
65. {
66. QMessageBox::information(this, tr("FTP"),tr("请检查ftp账号信息,用户名和密码都不允许为空。"));
67. return ;
68. }
69. }
70. //上述代码用来判断用户输入的用户名与密码是否可以登录ftp服务器
71.
72. if (ftpobj) {
73. ftpobj->abort();
74. ftpobj->deleteLater();
75. ftpobj = 0;
76. //![0]
77. ui->lineEdit_2->setEnabled(true);
78. ui->lineEdit_3->setEnabled(true);
79. ui->checkBox->setEnabled(true);
80. ui->treeWidget->setEnabled(false);
81. ui->pushButton->setEnabled(false);
82. ui->pushButton_3->setEnabled(false);
83. ui->pushButton_2->setEnabled(true);
84. ui->pushButton_2->setText(tr("Connect"));
85. ui->comboBox->setEditable(true);
86. ui->checkBox->setChecked(false);
87. #ifndef QT_NO_CURSOR
88. setCursor(Qt::ArrowCursor);
89. #endif
90. return;
91. }
92.
93. #ifndef QT_NO_CURSOR
94. setCursor(Qt::WaitCursor);
95. #endif
96.
97. ui->comboBox->setEditable(false);
98. ui->lineEdit_2->setEnabled(false);
99. ui->lineEdit_3->setEnabled(false);
100. ui->checkBox->setEnabled(false);
101. ui->treeWidget->setEnabled(true);
102. ui->pushButton_2->setEnabled(false);
103.
104. //![1]
105. ftpobj = new QFtp(this);
106.
107. connect(ftpobj, SIGNAL(commandFinished(int, bool)),
108. this, SLOT(ftpCommandFinished(int, bool)));
109. connect(ftpobj, SIGNAL(listInfo(constQUrlInfo&)),
110. this, SLOT(addToList(constQUrlInfo&)));
111. connect(ftpobj, SIGNAL(dataTransferProgress(qint64, qint64)),
112. this, SLOT(updateDataTransferProgress(qint64, qint64)));
113.
114. ui->treeWidget->clear();
115. ();
116. ();
117. //![1]
118.
119. //![2]
120. QUrlurl(ui->comboBox->currentText());
121. if (!d() || ().toLower() != QLatin1String("ftp")) {
122. ftpobj->connectToHost(ui->comboBox->currentText(), 21);
123. if(ui->checkBox->isChecked())
124. ftpobj->login();
125. else
126. ftpobj->login(ui->lineEdit_2->text(),ui->lineEdit_3->text());
127. } else {
128. ftpobj->connectToHost((), (21));
129.
130. /*if (!me().isEmpty())
131. //ftpobj->login(QUrl::fromPercentEncoding(me().toLatin1()),
rd());
132. ftpobj->login("company","123456");
133. else
134. ftpobj->login("company","123456");*/
135.
136. if(ui->checkBox->isChecked())
137. ftpobj->login();
138. else
139. ftpobj->login(ui->lineEdit_2->text(),ui->lineEdit_3->text());
140.
141. if (!().isEmpty())
142. ftpobj->cd(());
143. }
144. if( ui->radioButton_2->isChecked() )
145. ftpobj->setTransferMode(QFtp::Active);
146. else
147. ftpobj->setTransferMode(QFtp::Passive);
148. //![2]
149.
150. ui->label_2->setText(tr("Connecting to FTP server %1...")
151. .arg(ui->comboBox->currentText()));
152.
153. }
154.
155. //![3]
156. void Ftp::downloadFile()
157. {
158. QStringfileNam = ui->treeWidget->currentItem()->text(0);
159. //![3]
160. //
161. QStringfileName = QFileDialog::getExistingDirectory(this, tr("保存到。。。"),
162. ".",
163. QFileDialog::ShowDirsOnly
164. |
QFileDialog::DontResolveSymlinks);
165. if(()) return;
166.
167. fileName += "/";
168. fileName += fileNam;
169. if (QFile::exists(fileName)) {
170. int answer = QMessageBox::question(this,tr("FTP"),
171. tr("同名文件 %1已经存在,需要覆盖吗?").arg(fileName),
172. QMessageBox::Yes|QMessageBox::No);
173. if(answer == QMessageBox::Yes){
174. QDirdir(fileName);
175. (fileName);
176. }
177. if(answer == QMessageBox::No) return;
178. }
179. //![4]
180. file = new QFile(fileName);
181. if (!file->open(QIODevice::WriteOnly)) {
182. QMessageBox::information(this, tr("FTP"),
183. tr("Unable to save the file %1: %2.")
184. .arg(fileName).arg(file->errorString()));
185. delete file;
186. return;
187. }
188.
189. ftpobj->get(ui->treeWidget->currentItem()->text(0), file);
190.
191. progressDialog->setLabelText(tr("Downloading %1...").arg(fileName));
192. ui->pushButton_3->setEnabled(false);
193. progressDialog->exec();
194. }
195. //![4]
196. void Ftp::uploadFile()
197. {
198. // QStringDirName = ui->treeWidget->currentItem()->text(0);
199. //![3]
200. //
201. QStringfileName = QFileDialog::getOpenFileName(this, tr("打开文件。。。"),
202. ".",
203. "*");
204. if(()) return;
205.
206.
207. if (!QFile::exists(fileName)) {
208. return;
209. }
210. //![4]
211. file_upload = new QFile(fileName);
212. if (!file_upload->open(QIODevice::ReadOnly)) {
213. QMessageBox::information(this, tr("FTP"),
214. tr("Unable to open the file %1: %2.")
215. .arg(fileName).arg(file_upload->errorString()));
216. //delete file_upload;
217. return;
218. }
219.
220. //ftpobj->get(ui->treeWidget->currentItem()->text(0), file);
221. ftpobj->put(file_upload,fileName);
222. progressDialog->setLabelText(tr("Uploading %1...").arg(fileName));
223. progressDialog->exec();
224. }
225. //![5]
226. void Ftp::cancelDownload()
227. {
228. ftpobj->abort();
229. }
230. //![5]
231. //![6]
232. void Ftp::ftpCommandFinished(int, bool error)
233. {
234. #ifndef QT_NO_CURSOR
235. setCursor(Qt::ArrowCursor);
236. #endif
237.
238. if (ftpobj->currentCommand() == QFtp::ConnectToHost) {
239. if (error) {
240. QMessageBox::information(this, tr("FTP"),
241. tr("Unable to connect to the FTP server "
242. "at %1. Please check that the host "
243. "name is correct.")
244. .arg(ui->comboBox->currentText()));
245. connectOrDisconnect();
246. return;
247. }
248. ui->label_2->setText(tr("Logged onto %1.")
249. .arg(ui->comboBox->currentText()));
250. ui->treeWidget->setFocus();
251. ui->pushButton_3->setDefault(true);
252. ui->pushButton_2->setEnabled(true);
253. return;
254. }
255. //![6]
256. //![7]
257. if (ftpobj->currentCommand() == QFtp::Login)
258. ftpobj->list();
259. //![7]
260. //![8]
261. if (ftpobj->currentCommand() == QFtp::Get) {
262. if (error) {
263. ui->label_2->setText(tr("Canceled download of %1.")
264. .arg(file->fileName()));
265. file->close();
266. file->remove();
267. } else {
268. ui->label_2->setText(tr("Downloaded %1 to current directory.")
269. .arg(file->fileName()));
270. file->close();
271. }
272. delete file;
273. enableDownloadButton();
274. progressDialog->hide();
275. //![8]
276. //![9]
277. } else if (ftpobj->currentCommand() == QFtp::List) {
278. if (y()) {
279. ui->treeWidget->addTopLevelItem(new QTreeWidgetItem(QStringList() <
280. ui->treeWidget->setEnabled(false);
281. }
282. }
283. //![9]
284. }
285. //![10]
286. void Ftp::addToList(const QUrlInfo&urlInfo)
287. {
288. QTreeWidgetItem *item = new QTreeWidgetItem;
289. item->setText(0, ());
290. item->setText(1, QString::number(()));
291. item->setText(2, ());
292. item->setText(3, ());
293. item->setText(4, dified().toString("MMM ddyyyy"));
294.
295. QPixmappixmap(() ? ":/images/" : ":/images/");
296. item->setIcon(0, pixmap);
297.
298. isDirectory[()] = ();
299. ui->treeWidget->addTopLevelItem(item);
300. if (!ui->treeWidget->currentItem()) {
301. ui->treeWidget->setCurrentItem(ui->treeWidget->topLevelItem(0));
302. ui->treeWidget->setEnabled(true);
303. }
304. }
305. //![10]
306. //![11]
307. void Ftp::processItem(QTreeWidgetItem *item, int /*column*/)
308. {
309. QString name = item->text(0);
310. if ((name)) {
311. ui->treeWidget->clear();
312. ();
313. currentPath += "/" + name;
314. ftpobj->cd(name);
315. ftpobj->list();
316. ui->pushButton->setEnabled(true);
317. #ifndef QT_NO_CURSOR
318. setCursor(Qt::WaitCursor);
319. #endif
320. return;
321. }
322. }
323. //![11]
324. //![12]
325. void Ftp::cdToParent()
326. {
327. #ifndef QT_NO_CURSOR
328. setCursor(Qt::WaitCursor);
329. #endif
330. ui->treeWidget->clear();
331. ();
332. currentPath = (dexOf('/'));
333. if (y()) {
334. ui->pushButton->setEnabled(false);
335. ftpobj->cd("/");
336. } else {
337. ftpobj->cd(currentPath);
338. }
339. ftpobj->list();
340. }
341. //![12]
342. //![13]
343. void Ftp::updateDataTransferProgress(qint64 readBytes,
344. qint64 totalBytes)
345. {
346. progressDialog->setMaximum(totalBytes);
347. progressDialog->setValue(readBytes);
348. }
349. //![13]
350. //![14]
351. void Ftp::enableDownloadButton()
352. {
353. QTreeWidgetItem *current = ui->treeWidget->currentItem();
354. if (current) {
355. QStringcurrentFile = current->text(0);
356. ui->pushButton_3->setEnabled(!(currentFile));
357. } else {
358. ui->pushButton_3->setEnabled(false);
359. }
360. }
361. //![14]
362.
363. void Ftp::enableCheckBox()
364. {
365.
366. if (ui->checkBox->isChecked()) {
367. ui->lineEdit_2->setEnabled(false);
368. ui->lineEdit_3->setEnabled(false);
369. } else {
370. ui->lineEdit_2->setEnabled(true);
371. ui->lineEdit_3->setEnabled(true);
372. }
373. }
374. /*
375. void Ftp::mousePressEvent(QMouseEvent *event)
376. {
377. if (event->button() == Qt::LeftButton)
378. startPos = event->pos();
379. QWidget::mousePressEvent(event);
380. }
381.
382. void Ftp::mouseMoveEvent(QMouseEvent *event)
383. {
384. if (event->buttons() &Qt::LeftButton) {
385. int distance = (event->pos() - startPos).manhattanLength();
386. if (distance >= QApplication::startDragDistance())
387. downloadFiles();
388. }
389. QWidget::mouseMoveEvent(event);
390. }
391.
392. void Ftp::dragEnterEvent(QDragEnterEvent *event)
393. {
394. Ftp *source =
395. qobject_cast
396. if (source && source != this) {
397. event->setDropAction(Qt::MoveAction);
398. event->accept();
399. }
400. }
401. void Ftp::dragMoveEvent(QDragMoveEvent *event)
402. {
403. Ftp *source =
404. qobject_cast
405. if (source && source != this) {
406. event->setDropAction(Qt::MoveAction);
407. event->accept();
408. }
409. }
410. void Ftp::dropEvent(QDropEvent *event)
411. {
412. Ftp *source =
413. qobject_cast
414. if (source && source != this) {
415. //addItem(event->mimeData()->text());
416. event->setDropAction(Qt::MoveAction);
417. event->accept();
418. }
419. }
420. */
3.运行效果图
左图显示为FTP客户端开始在FTP服务器上下载文件


发布评论