Download a file from a FTP Server

部署运行你感兴趣的模型镜像
{**************************************************************}
  The following function shows how to connect to a ftp server
  and download a file.

  Specify the Host, User, Password, Port, Ftp-Directory,
  File on Ftp, the local file.
  
  You need a TProgressBar to show the progress and a TLabel to
  show some progress informations.

  Note: The functions from wininet.dll are used.

{**************************************************************}

uses
  WinInet, ComCtrls;

function FtpDownloadFile(strHost, strUser, strPwd: string;
  Port: Integer; ftpDir, ftpFile, TargetFile: string; ProgressBar: TProgressBar): Boolean;

  function FmtFileSize(Size: Integer): string;
  begin
    if Size >= $F4240 then
      Result := Format('%.2f', [Size / $F4240]) + ' Mb'
    else
    if Size < 1000 then
      Result := IntToStr(Size) + ' bytes'
    else
      Result := Format('%.2f', [Size / 1000]) + ' Kb';
  end;

const
  READ_BUFFERSIZE = 4096;  // or 256, 512, ...
var
  hNet, hFTP, hFile: HINTERNET;
  buffer: array[0..READ_BUFFERSIZE - 1] of Char;
  bufsize, dwBytesRead, fileSize: DWORD;
  sRec: TWin32FindData;
  strStatus: string;
  LocalFile: File;
  bSuccess: Boolean;
begin
  Result := False;

  { Open an internet session }
  hNet := InternetOpen('Program_Name', // Agent
                        INTERNET_OPEN_TYPE_PRECONFIG, // AccessType
                        nil,  // ProxyName
                        nil, // ProxyBypass
                        0); // or INTERNET_FLAG_ASYNC / INTERNET_FLAG_OFFLINE

  {
    Agent contains the name of the application or
    entity calling the Internet functions
  }


  { See if connection handle is valid }
  if hNet = nil then
  begin
    ShowMessage('Unable to get access to WinInet.Dll');
    Exit;
  end;

  { Connect to the FTP Server }
  hFTP := InternetConnect(hNet, // Handle from InternetOpen
                          PChar(strHost), // FTP server
                          port, // (INTERNET_DEFAULT_FTP_PORT),
                          PChar(StrUser), // username
                          PChar(strPwd),  // password
                          INTERNET_SERVICE_FTP, // FTP, HTTP, or Gopher?
                          0, // flag: 0 or INTERNET_FLAG_PASSIVE
                          0);// User defined number for callback

  if hFTP = nil then
  begin
    InternetCloseHandle(hNet);
    ShowMessage(Format('Host "%s" is not available',[strHost]));
    Exit;
  end;

  { Change directory }
  bSuccess := FtpSetCurrentDirectory(hFTP, PChar(ftpDir));

  if not bSuccess then
  begin
    InternetCloseHandle(hFTP);
    InternetCloseHandle(hNet);
    ShowMessage(Format('Cannot set directory to %s.',[ftpDir]));
    Exit;
  end;

  { Read size of file }
  if FtpFindFirstFile(hFTP, PChar(ftpFile), sRec, 0, 0) <> nil then
  begin
    fileSize := sRec.nFileSizeLow;
    // fileLastWritetime := sRec.lastWriteTime
  end else
  begin
    InternetCloseHandle(hFTP);
    InternetCloseHandle(hNet);
    ShowMessage(Format('Cannot find file ',[ftpFile]));
    Exit;
  end;

  { Open the file }
  hFile := FtpOpenFile(hFTP, // Handle to the ftp session
                       PChar(ftpFile), // filename
                       GENERIC_READ, // dwAccess
                       FTP_TRANSFER_TYPE_BINARY, // dwFlags
                       0); // This is the context used for callbacks.

  if hFile = nil then
  begin
    InternetCloseHandle(hFTP);
    InternetCloseHandle(hNet);
    Exit;
  end;

  { Create a new local file }
  AssignFile(LocalFile, TargetFile);
  {$i-}
  Rewrite(LocalFile, 1);
  {$i+}

  if IOResult <> 0 then
  begin
    InternetCloseHandle(hFile);
    InternetCloseHandle(hFTP);
    InternetCloseHandle(hNet);
    Exit;
  end;

  dwBytesRead := 0;
  bufsize := READ_BUFFERSIZE;

  while (bufsize > 0) do
  begin
    Application.ProcessMessages;

    if not InternetReadFile(hFile,
                            @buffer, // address of a buffer that receives the data
                            READ_BUFFERSIZE, // number of bytes to read from the file
                            bufsize) then Break; // receives the actual number of bytes read

    if (bufsize > 0) and (bufsize <= READ_BUFFERSIZE) then
      BlockWrite(LocalFile, buffer, bufsize);
    dwBytesRead := dwBytesRead + bufsize;

    { Show Progress }
    ProgressBar.Position := Round(dwBytesRead * 100 / fileSize);
    Form1.Label1.Caption := Format('%s of %s / %d %%',[FmtFileSize(dwBytesRead),FmtFileSize(fileSize) ,ProgressBar.Position]);
  end;

  CloseFile(LocalFile);

  InternetCloseHandle(hFile);
  InternetCloseHandle(hFTP);
  InternetCloseHandle(hNet);
  Result := True;
end;

您可能感兴趣的与本文相关的镜像

Python3.10

Python3.10

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

内容概要:本文介绍了一个基于Matlab的综合能源系统优化调度仿真资源,重点实现了含光热电站、有机朗肯循环(ORC)和电含光热电站、有机有机朗肯循环、P2G的综合能源优化调度(Matlab代码实现)转气(P2G)技术的冷、热、电多能互补系统的优化调度模型。该模型充分考虑多种能源形式的协同转换与利用,通过Matlab代码构建系统架构、设定约束条件并求解优化目标,旨在提升综合能源系统的运行效率与经济性,同时兼顾灵活性供需不确定性下的储能优化配置问题。文中还提到了相关仿真技术支持,如YALMIP工具包的应用,适用于复杂能源系统的建模与求解。; 适合人群:具备一定Matlab编程基础和能源系统背景知识的科研人员、研究生及工程技术人员,尤其适合从事综合能源系统、可再生能源利用、电力系统优化等方向的研究者。; 使用场景及目标:①研究含光热、ORC和P2G的多能系统协调调度机制;②开展考虑不确定性的储能优化配置与经济调度仿真;③学习Matlab在能源系统优化中的建模与求解方法,复现高水平论文(如EI期刊)中的算法案例。; 阅读建议:建议读者结合文档提供的网盘资源,下载完整代码和案例文件,按照目录顺序逐步学习,重点关注模型构建逻辑、约束设置与求解器调用方式,并通过修改参数进行仿真实验,加深对综合能源系统优化调度的理解。
在 SQL Server 中直接配置 FTP 服务并不可行,因为 SQL Server 本身并不提供 FTP 功能。然而,可以通过外部工具和脚本实现将 SQL Server 的数据库备份文件传输到远程 FTP 服务器。以下是配置 SQL Server 自动备份并通过 FTP 远程传输的完整流程: ### 1. 配置本地数据库备份路径 首先,确保 SQL Server 有本地存储数据库备份的目录。例如: ```sql -- 设置备份目录 DECLARE @backupPath NVARCHAR(255) = 'E:\test\sqlbak\'; ``` ### 2. 编写备份脚本 编写 SQL 脚本以循环备份多个数据库,并将备份文件存储在本地目录中: ```sql -- 循环备份多个数据库 DECLARE @str NVARCHAR(MAX) = ''; SELECT @str = @str + 'BACKUP DATABASE ' + name + ' TO DISK = ''' + @backupPath + name + '_' + REPLACE(REPLACE(REPLACE(CONVERT(NVARCHAR(16), GETDATE(), 120), '-', '_'), ':', ''), ' ', '_') + '.bak'' WITH COMPRESSION, INIT;' FROM sys.databases WHERE name IN ('dev', 'weekseo'); -- 替换为需要备份的数据库名称 EXEC (@str); ``` ### 3. 使用 `xp_cmdshell` 调用 FTP 命令 在 SQL Server 中启用 `xp_cmdshell`,以便调用外部命令行工具,例如 FTP 客户端: ```sql -- 启用 xp_cmdshell(需要管理员权限) EXEC sp_configure 'show advanced options', 1; RECONFIGURE; EXEC sp_configure 'xp_cmdshell', 1; RECONFIGURE; ``` 然后,通过 `xp_cmdshell` 调用 FTP 命令,将备份文件传输到远程 FTP 服务器: ```sql -- 调用 FTP 客户端传输备份文件 EXEC xp_cmdshell 'ftp -s:E:\test\ftpconfig.txt 127.0.0.1'; ``` 其中,`E:\test\ftpconfig.txt` 是 FTP 配置文件,包含用户名、密码以及 FTP 服务器的地址信息。 ### 4. 删除过期备份文件 为了节省磁盘空间,可以使用 `xp_delete_file` 存储过程删除过期的备份文件: ```sql -- 删除 10 天前的备份文件 DECLARE @Date DATETIME; SELECT @Date = GETDATE() - 10; EXECUTE master.dbo.xp_delete_file 0, N'E:\test\sqlbak', N'bak', @Date; ``` ### 5. 配置 FTP 服务器 如果尚未配置 FTP 服务器,可以使用 FileZilla Server 搭建 FTP 服务。FileZilla Server 是一个免费且功能强大的 FTP 服务器软件,支持 Windows 平台[^2]。下载地址:[https://www.filezilla.cn/download/server](https://www.filezilla.cn/download/server) 在配置 FTP 服务器时,确保以下几点: - 配置用户账户和权限,确保 SQL Server 有权限上传文件。 - 设置 FTP 服务器的根目录,确保备份文件可以上传到指定路径。 - 开放必要的 FTP 端口(默认为 21),并确保防火墙允许该端口通信。 ### 6. 确保网络通信正常 在 SQL ServerFTP 服务器之间确保网络通信正常,并开放以下端口: - **1433**:SQL Server 默认端口 - **21**:FTP 服务端口 - **其他必要端口**:如 4022、135、1434 等[^2] ### 7. 设置定时任务 最后,可以将上述脚本集成到 SQL Server Agent 中,并设置定时任务,实现自动备份和 FTP 传输。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值