# FTP 上传脚本 (UTF-8 BOM) - 健壮版 $FtpHost="116.198.221.125" $FtpPort=21 $FtpUser="wenchuanyi" $FtpPass="r7P^f*v7rFts" $RemoteRoot="/" $Stage = Join-Path $env:TEMP "wcy_publish_staging" function New-FtpReq([string]$path,[string]$method){ $uri="ftp://${FtpHost}:${FtpPort}/"+$path.TrimStart('/') $r=[System.Net.FtpWebRequest]::Create($uri) $r.Method=$method $r.Credentials=New-Object System.Net.NetworkCredential($FtpUser,$FtpPass) $r.UsePassive=$true $r.UseBinary=$true $r.KeepAlive=$false $r.Timeout=120000 return $r } function MkDir([string]$p){ $r=New-FtpReq $p ([System.Net.WebRequestMethods+Ftp]::MakeDirectory) try{ $null=$r.GetResponse() }catch{} } function Exist([string]$p){ $r=New-FtpReq $p ([System.Net.WebRequestMethods+Ftp]::GetDateTimestamp) try{ $null=$r.GetResponse(); return $true }catch{ return $false } } function UpFile([string]$lp,[string]$rp){ $r=New-FtpReq $rp ([System.Net.WebRequestMethods+Ftp]::UploadFile) $b=[System.IO.File]::ReadAllBytes($lp) $r.ContentLength=$b.Length $s=$r.GetRequestStream() try{ $s.Write($b,0,$b.Length) }finally{ $s.Close() } $resp=$r.GetResponse() try{ $resp.Close() }catch{} } function UpDir([string]$ld,[string]$rd){ foreach($d in Get-ChildItem $ld -Directory){ $c="$rd/$($d.Name)" if(-not (Exist $c)){ MkDir $c } UpDir $d.FullName $c } foreach($f in Get-ChildItem $ld -File){ $rf="$rd/$($f.Name)" try{ if(-not (Exist $rf)){ UpFile $f.FullName $rf; Write-Host "UP $rf" } else { Write-Host "SKIP(exists) $rf" } }catch{ Write-Host "RETRY $rf" try{ UpFile $f.FullName $rf; Write-Host "OK $rf" }catch{ Write-Host "FAIL $rf : $_" } } } } if(-not (Test-Path $Stage)){ Write-Host "STAGING_MISSING"; exit 1 } if(-not (Exist $RemoteRoot)){ MkDir $RemoteRoot } UpDir $Stage $RemoteRoot Write-Host "FTP_UPLOAD_DONE" Remove-Item $Stage -Recurse -Force