在windows下装了个linux虚拟机,两者之间传输文件挺麻烦的.写了个简单的文件传输程序,来方便自己数据传送.
server 端:
import
java.io.BufferedReader;
import
java.io.BufferedWriter;
import
java.io.File;
import
java.io.FileOutputStream;
import
java.io.InputStream;
import
java.io.InputStreamReader;
import
java.io.OutputStreamWriter;
import
java.io.PrintWriter;
import
java.net.ServerSocket;
import
java.net.Socket;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
class
CPThread
extends
Thread
...
{
Socket socket = null;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public CPThread(Socket socket)...{
this.socket = socket;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void run()...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
try...{
InputStream in =
socket.getInputStream();
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())),true);
while(true)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//第一个參数为命令
byte cmd[] = new byte[128];
int b = 0;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
while(b<cmd.length)...{
b += in.read(cmd, b, cmd.length-b);
}
int ends = 0;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
for(int i=0;i<cmd.length;i++)...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if(cmd[i]=='-0')...{
ends = i;
break;
}
}
String cmds = new String(cmd,0,ends);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if("exit".equals(cmds))...{
System.exit(0);
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
else if("cp".equals(cmds))...{
byte[] filename = new byte[256];
b = 0;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
while(b<filename.length)...{
b += in.read(filename, b, filename.length-b);
}
ends = 0;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
for(int i=0;i<filename.length;i++)...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if(filename[i]=='-0')...{
ends = i;
break;
}
}
String filenames = new String(filename,0,ends);
File fileout = new File(filenames);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if(fileout.isFile())...{
throw new Exception("file exists"+fileout.getAbsolutePath());
}
FileOutputStream fos = new FileOutputStream(fileout);
byte[] filesize = new byte[64];
b = 0;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
while(b<filesize.length)...{
b += in.read(filesize, b, filesize.length-b);
}
ends = 0;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
for(int i=0;i<filesize.length;i++)...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if(filesize[i]=='-0')...{
ends = i;
break;
}
}
String filesizes = new String(filesize,0,ends);
System.out.println("filesize:"+filesizes);
int ta = Integer.parseInt(filesizes);
byte[] buf = new byte[1024*10];
//InputStream ins = socket.getInputStream();
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
while(true)...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if(ta==0)...{
break;
}
int len = ta;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if(len>buf.length)...{
len = buf.length;
}
int rlen = in.read(buf, 0, len);
//int rlen = ins.read(buf, 0, len);
ta -= rlen;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if(rlen>0)...{
fos.write(buf,0,rlen);
fos.flush();
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
else...{
break;
}
}
out.println("cp finish!");
fos.close();
break;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
else...{
System.out.println("err command!");
out.println("err command!");
break;
}
}
socket.close();
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
}catch(Exception e)...{
e.printStackTrace();
}
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
JCPServer
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//**
* @param args
*/
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public static void main(String[] args) throws Exception ...{
// TODO Auto-generated method stub
int num = args.length;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if(num<=0)...{
System.out.println("please input server port!");
System.exit(0);
}
int port = Integer.parseInt(args[0]);
System.out.println("you input port:"+port);
//绑定接受数据端口
ServerSocket s = new ServerSocket(port);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
while(true)...{
Socket socket = s.accept();
new CPThread(socket).start();
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
client 端
import
java.io.BufferedWriter;
import
java.io.File;
import
java.io.FileInputStream;
import
java.io.OutputStream;
import
java.io.OutputStreamWriter;
import
java.io.PrintWriter;
import
java.net.InetAddress;
import
java.net.Socket;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
JCPClient
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//**
* @param args
*/
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public static void main(String[] args)throws Exception ...{
// TODO Auto-generated method stub
int num = args.length;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if(num<=3)...{
System.out.println("please input server ip port filefrom fileto!");
System.exit(0);
}
System.out.println("host:"+args[0]);
System.out.println("port:"+args[1]);
System.out.println("filefrom:"+args[2]);
System.out.println("fileto:"+args[3]);
InetAddress addr = InetAddress.getByName(args[0]);
Socket socket =
new Socket(addr, Integer.parseInt(args[1]));
OutputStream out = socket.getOutputStream();
byte[] cmd = new byte[128];
byte[] tcmd = "cp".getBytes();
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
for(int i=0;i<tcmd.length;i++)...{
cmd[i] = tcmd[i];
}
cmd[tcmd.length] = '-0';
out.write(cmd,0,cmd.length);
//文件名称
byte[] file = new byte[256];
byte[] tfile = args[3].getBytes();
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
for(int i=0;i<tfile.length;i++)...{
file[i] = tfile[i];
}
file[tfile.length] = '-0';
out.write(file,0,file.length);
//大小
File filein = new File(args[2]);
byte[] size = new byte[64];
byte[] tsize = (""+filein.length()).getBytes();
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
for(int i=0;i<tsize.length;i++)...{
size[i] = tsize[i];
}
size[tsize.length] = '-0';
out.write(size,0,size.length);
FileInputStream fis = null;
byte[] buf = new byte[1024*10];
//char[] bufC = new char[1024*10];
fis = new FileInputStream(filein);
int readsize = 0;
//OutputStream ops = socket.getOutputStream();
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
while((readsize = fis.read(buf, 0, buf.length))>0)...{
out.write(buf,0,readsize);
out.flush();
}
socket.close();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}