如何使用winrm4j Java库实现文件上传与下载?
使用winrm4j实现文件上传与下载
winrm4j本身并未提供专门的文件上传下载API,但可以通过执行PowerShell命令的方式,实现和你示例中类似的文件传输功能,以下是具体实现方案:
一、文件上传
方法1:模拟PowerShell会话复制(和你示例逻辑一致)
通过winrm4j执行PowerShell脚本,创建远程会话并复制文件:
import com.cloudbees.jenkins.plugins.winrm.WinRMClient; import com.cloudbees.jenkins.plugins.winrm.WinRMException; public class WinRmFileUpload { public static void main(String[] args) throws WinRMException { // 初始化WinRM客户端 WinRMClient client = WinRMClient.builder() .url("http://<remoteComputerName>:5985/wsman") .credentials("<username>", "<password>") .build(); // 定义PowerShell脚本,替换占位符 String localFile = "<localFileLocation>"; String remoteFile = "<futurRemoteFileLocation>"; String psScript = String.format( "$creds = New-Object System.Management.Automation.PSCredential('%s', (ConvertTo-SecureString '%s' -AsPlainText -Force)); " + "$session = New-PSSession -ComputerName '%s' -Credential $creds; " + "Copy-Item -Path '%s' -Destination '%s' -ToSession $session; " + "Remove-PSSession $session", "<username>", "<password>", "<remoteComputerName>", localFile, remoteFile ); // 执行脚本 client.executePs(psScript); client.close(); } }
注意:这种方式依赖远程主机启用了PowerShell会话功能,且网络允许会话建立。
方法2:Base64编码文件内容(适合小文件)
将本地文件编码为Base64字符串,通过PowerShell脚本写入远程文件:
import com.cloudbees.jenkins.plugins.winrm.WinRMClient; import com.cloudbees.jenkins.plugins.winrm.WinRMException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Base64; public class WinRmBase64Upload { public static void main(String[] args) throws Exception { WinRMClient client = WinRMClient.builder() .url("http://<remoteComputerName>:5985/wsman") .credentials("<username>", "<password>") .build(); // 读取本地文件并编码为Base64 byte[] fileBytes = Files.readAllBytes(Paths.get("<localFileLocation>")); String base64Content = Base64.getEncoder().encodeToString(fileBytes); String remotePath = "<futurRemoteFileLocation>"; // PowerShell脚本:解码并写入文件 String psScript = String.format( "$content = [Convert]::FromBase64String('%s'); " + "Set-Content -Path '%s' -Value $content -Encoding Byte", base64Content, remotePath ); client.executePs(psScript); client.close(); } }
二、文件下载
方法:远程读取文件为Base64,本地解码保存
通过PowerShell读取远程文件并转为Base64,Java端获取输出后解码保存:
import com.cloudbees.jenkins.plugins.winrm.WinRMClient; import com.cloudbees.jenkins.plugins.winrm.WinRMException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Base64; public class WinRmFileDownload { public static void main(String[] args) throws Exception { WinRMClient client = WinRMClient.builder() .url("http://<remoteComputerName>:5985/wsman") .credentials("<username>", "<password>") .build(); String remoteFile = "<remoteFileLocation>"; // PowerShell脚本:读取文件并输出Base64内容 String psScript = String.format( "$content = [Convert]::ToBase64String((Get-Content -Path '%s' -Encoding Byte)); " + "Write-Output $content", remoteFile ); // 执行脚本并获取输出 String base64Output = client.executePs(psScript).getStdout().trim(); // 解码并保存到本地 byte[] fileBytes = Base64.getDecoder().decode(base64Output); Files.write(Paths.get("<localSaveLocation>"), fileBytes); client.close(); } }
注意事项
- 对于大文件,Base64编码方式可能存在性能问题,建议使用会话复制的方式,或对Base64内容分块处理。
- 确保远程主机的WinRM服务已正确配置,允许执行PowerShell命令,且目标文件路径具备对应读写权限。
内容的提问来源于stack exchange,提问作者Mouti
相关产品推荐
相关产品推荐

