上一篇操作文件夹都是基于本地操作,能不能使用别的计算机操作自己电脑的文件夹?这就需要使用java的网络编程.还是先看效果图:首先是通过客户端传入参数,服务端接收后进行文件夹的操作:
列出所有文件:
下面是代码实现:这是服务端代码
import java.net.*;
import java.io.*;
public class GreetingServer extends Thread
{
boolean success = false;
private ServerSocket serverSocket;
public GreetingServer(int port) throws IOException
{
serverSocket = new ServerSocket(port);
//serverSocket.setSoTimeout(10000);
//10.176.65.142 127.0.0.1
}
public void run()
{
while(true)
{
try
{
System.out.println("等待远程连接,端口号为:" + serverSocket.getLocalPort() + "...");
Socket server = serverSocket.accept();
System.out.println("远程主机地址:" + server.getRemoteSocketAddress());
DataInputStream in = new DataInputStream(server.getInputStream());
StringBuffer request = new StringBuffer(2048);
//从客户端输入的数据
//System.out.println(in.readUTF());
String[] str = in.readUTF().split(" ");
//判断str中第一个参数是什么命令调用相应方法
if (success == false){
respose(str);
}
DataOutputStream out = new DataOutputStream(server.getOutputStream());
//回应客户端数据
out.writeUTF("操作成功:" + server.getLocalSocketAddress() + "\nGoodbye!");
server.close();
}catch(SocketTimeoutException s)
{
System.out.println("Socket timed out!");
break;
}catch(IOException e)
{
e.printStackTrace();
break;
}
}
}
private boolean respose(String[] str) {
if (str == null){
return true;
}
String code = str[0];
switch (code){
case "copy" :
DocUtil.copy(str[1],str[2]);
break;
case "delete":
DocUtil.delete(str[1]);
break;
case "dir":
DocUtil.dir(str[1]);
break;
}
return true;
}
public static void main(String [] args)
{
int port = 888;
try
{
Thread t = new GreetingServer(port);
t.run();
}catch(IOException e)
{
e.printStackTrace();
}
}
}
客户端代码:
import java.net.*;
import java.io.*;
public class GreetingClient {
//args参数标准 copy F:\testJVM F:\其他 执行复制操作
// delete 文件路径 执行删除操作
// dir 文件路径 列出所有文件和文件夹
public static void main(String [] args) {
//String[] arguments = new String[args.length];
String str = "";
for(int i =0; i<args.length;i++){
if(i==args.length -1) {
str += args[i];
}else {
str += args[i] + " ";
}
}
String serverName = "localhost";
int port = 888;
try {
System.out.println("连接到主机:" + serverName + " ,端口号:" + port);
Socket client = new Socket(serverName, port);
System.out.println("远程主机地址:" + client.getRemoteSocketAddress());
OutputStream outToServer = client.getOutputStream();
DataOutputStream out = new DataOutputStream(outToServer);
//向服务器端传输数据
//out.writeUTF("Hello from " + client.getLocalSocketAddress());
out.writeUTF(str);
InputStream inFromServer = client.getInputStream();
DataInputStream in = new DataInputStream(inFromServer);
System.out.println(in.readUTF());
client.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
实际操作类代码:
import java.io.*;
public class DocUtil {
/**
* dir 方法列出目录下所有文件和文件夹
* @param str
*/
public static void dir(String str){
File file = new File(str);
list(file);
}
/**
* copy方法
* @param src 源路径
* @param dest 目标路径
*/
public static void copy(String src ,String dest){
copyDirAndFire(new File(src),new File(dest));
}
public static void delete(String str){
deleteDir(new File(str));
}
public static void deleteEmptyDir(File file){
file.delete();
}
//递归删除文件夹
public static void deleteDir(File file){
//判断是否为文件
if(file.isDirectory()){
//列出所有的子文件
File[] files = file.listFiles();
if(files!=null){
for (int i = 0; i < files.length; i++) {
//删除子文件
if (files[i].isFile()) {
deleteEmptyDir(files[i]);
} //删除子目录
else {
deleteDir(files[i]);
}
}
}
deleteEmptyDir(file);
}else{
deleteEmptyDir(file);
}
}
/**
*实现文件拷贝
* @param src 源路径
* @param dest 目标路径
*/
public static void copyFile(File src,File dest){
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(src);
out = new FileOutputStream(dest);
int len = 0;
byte[] car = new byte[1024];
while((len = in.read(car))!=-1){
out.write(car, 0, len);
out.flush();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
*实现文件夹拷贝
* @param src 源路径
* @param dest 目标路径
*/
public static void copyDirAndFire(File src, File dest){
//判断源路径是否为空
if(src==null){
return;
}
//构造目标路径
File target = new File(dest,src.getName());
//判断源路径是否为文件夹
if(src.isDirectory()){
target.mkdirs();
File[] files = src.listFiles();
for(File f:files){
copyDirAndFire(f, target);
}
}else{
Copy.copyFile(src, target);
}
}
public static void list(File file){
if(file.isDirectory())//判断file是否是目录
{
File [] lists = file.listFiles();
if(lists!=null)
{
for(int i=0;i<lists.length;i++)
{
list(lists[i]);//是目录就递归进入目录内再进行判断
}
}
}
System.out.println(file);//file不是目录,就输出它的路径名,这是递归的出口
}
}