#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFtp>
#include <QDebug>
#include <QFile>
#include <QDataStream>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QFtp *ftp = new QFtp(this);
ftp->connectToHost("192.168.1.99", 21);
ftp->login();
ftp->cd("/mtp/blk/");
//将"0x10"以二进制的方式写到"ftp://192.168.1.99/mtp/blk/2.txt"中
int a = 0x10;
uchar *p = (uchar *)&a;
uchar bf[10];
bf[0] = p[0];
bf[1] = p[1];
bf[2] = p[2];
bf[3] = p[3];
bf[4] = ' ';
qDebug() << p[0] << p[1] << p[2] << p[3];
ftp->put((const char*)bf, "2.txt");
//将服务器上的文件下载到本地
QFile *file;
file = new QFile("2.txt");
if(!file->open(QIODevice::WriteOnly))
qDebug() << "服务器上的文件打开失败";
ftp->get("2.txt", file);
file->close();
//下载到本地后打开,读出数据
if(!file->open(QIODevice::ReadOnly))
qDebug() << "本地文件打开失败";
char bf1[10];
QDataStream in(&file);
in.readRawData((char *)&bf, 4);
file->close();
int aa;
memcpy(&a, bf1, 4);
qDebug() << aa;
ftp->close();
}
MainWindow::~MainWindow()
{
delete ui;
}
运行结果:
16 0 0 0
16