package com.xx.util;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang.StringUtils;
/**
* 在win端使用工具类上传文件和文件夹到Linux服务器的指定目录中
* @author Administrator
*
*/
public class TransferUtil {
public static void execCommand(String localPath,String userName,String password,String remoteIp,String remotePath)
{
StringBuffer sb = new StringBuffer();
sb.append("cmd /C pscp");
if (StringUtils.isNotBlank(userName))
{
sb.append(" -l ").append(userName);
}
else
{
System.out.println("远端用户名不能为空!");
return;
}
if (StringUtils.isNotBlank(password))
{
sb.append(" -pw ").append(password);
}
else
{
System.out.println("远端密码不能为空!");
return;
}
sb.append(" -r -v");//支持目录的上传
if (StringUtils.isNotBlank(localPath))
{
sb.append(" ").append(localPath);
}
else
{
System.out.println("本地路径不能为空!");
return;
}
if (StringUtils.isNotBlank(remoteIp))
{
sb.append(" ").append(remoteIp).append(":");
}
else
{
System.out.println("远端IP不能为空!");
return;
}
if (StringUtils.isNotBlank(remotePath))
{
if (remotePath.indexOf('/') != -1)
{
sb.append(remotePath);
}
else
{
sb.append("/").append(remotePath);
}
}
else
{
System.out.println("远端路径不能为空!");
return;
}
System.out.println("最终的执行命令为:"+sb.toString());
try {
Process process = Runtime.getRuntime().exec(sb.toString());
new Thread(new ThreadUtil(process.getInputStream())).start();
new Thread(new ThreadUtil(process.getErrorStream())).start();
process.getOutputStream().close();
int exitValue = process.waitFor();
System.out.println("返回值:" + exitValue);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 将多个目录下的文件上传到远端服务器上的指定目录中
* @param localFileList
* @param userName
* @param password
* @param remoteIp
* @param remotePath
*/
public static void execMultiCommand(List<String> localFileList,String userName,String password,String remoteIp,String remotePath)
{
if (null != localFileList && !localFileList.isEmpty())
{
for (String filePath : localFileList)
{
execCommand(filePath, userName, password, remoteIp, remotePath);
}
}
}
/**
* 将多个目录下的文件上传到远端服务器上的指定目录中
* @param localFileArray
* @param userName
* @param password
* @param remoteIp
* @param remotePath
*/
public static void execMultiCommand(String[] localFileArray,String userName,String password,String remoteIp,String remotePath)
{
if (null != localFileArray)
{
execMultiCommand(Arrays.asList(localFileArray), userName, password, remoteIp, remotePath);
}
}
/**
* 执行上传操作的方法,值传入待上传的文件列表
* @param localFileList
*/
public static void execMultiCommand(List<String> localFileList)
{
List<String[]> retList = FileUtils.getNginxConfig();
for (String[] strings : retList)
{
execMultiCommand(localFileList, strings[0],strings[1],strings[2],strings[3]);
}
}
/**
* 执行上传操作的方法,值传入待上传的文件列表和远程部分路径
* @param localFileList
* @param remotePath
*/
public static void execMultiCommand(List<String> localFileList,String remotePath)
{
List<String[]> retList = FileUtils.getNginxConfig();
for (String[] strings : retList)
{
execMultiCommand(localFileList, strings[0],strings[1],strings[2],strings[3] + "/" + remotePath);
}
}
/**
* 执行上传操作的方法,值传入待上传的文件列表和远程部分路径
* @param localFileArray
* @param remotePath
*/
public static void execMultiCommand(String[] localFileArray,String remotePath)
{
List<String[]> retList = FileUtils.getNginxConfig();
for (String[] strings : retList)
{
execMultiCommand(localFileArray, strings[0],strings[1],strings[2],strings[3] + "/" + remotePath);
}
}
/**
* 执行上传操作的方法,值传入待上传的文件列表
* @param localFileArray
*/
public static void execMultiCommand(String[] localFileArray)
{
List<String[]> retList = FileUtils.getNginxConfig();
for (String[] strings : retList)
{
execMultiCommand(localFileArray, strings[0],strings[1],strings[2],strings[3]);
}
}
/**
* 上传单个文件或者文件夹
* @param localFile
*/
public static void execCommand(String localFile)
{
List<String[]> retList = FileUtils.getNginxConfig();
for (String[] strings : retList)
{
execCommand(localFile, strings[0],strings[1],strings[2],strings[3]);
}
}
}
其中ThreadUtil如下:
package com.xx.util;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* 线程辅助类,用来读取输入流
* @author Administrator
*
*/
public class ThreadUtil implements Runnable
{
private InputStream ins;
public ThreadUtil(InputStream ins) {
this.ins = ins;
}
public void run() {
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(ins));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
其中FileUtils.getNginxConfig()的代码如下:
/**
* 获取Nginx的配置信息
* @return ret List<String[]>
*/
public static List<String[]> getNginxConfig()
{
List<String[]> ret = new ArrayList<String[]>();
Properties p = new Properties();
try {
p.load(FileUtils.class.getResourceAsStream("/com/sac/site/util/common.properties"));
String userName = p.getProperty("nginx_username", "root").trim();
String password = p.getProperty("nginx_password", "root").trim();
String serverIp = p.getProperty("nginx_serverIp", "172.18.121.72").trim();
String serverPath = p.getProperty("nginx_serverPath", "/root/nginx").trim();
if (-1 != userName.indexOf(','))
{
String[] userNames = userName.split(",");
String[] passwords = password.split(",");
String[] serverIps = serverIp.split(",");
String[] serverPaths = serverPath.split(",");
for (int i = 0; i < userNames.length; i++)
{
String[] data = new String[4];
data[0] = userNames[i];
data[1] = passwords[i];
data[2] = serverIps[i];
data[3] = serverPaths[i];
ret.add(data);
}
}
else
{
String[] data = new String[4];
data[0] = userName;
data[1] = password;
data[2] = serverIp;
data[3] = serverPath;
ret.add(data);
}
} catch (IOException e) {
e.printStackTrace();
}
return ret;
}
在调用时候很简单,直接传入对应的参数即可。比如:
TransferUtil.execCommand("c:\\test.txt","root","root","192.168.7.7","/root/upload").