码迷,mamicode.com
首页 > 编程语言 > 详细

java实现文件传输

时间:2014-10-09 20:57:37      阅读:303      评论:0      收藏:0      [点我收藏+]

标签:style   http   color   io   os   ar   java   for   文件   

在windows下装了个linux虚拟机,两者之间传输文件挺麻烦的.写了个简单的文件传输程序,来方便自己数据传送.

server 端:

 

bubuko.com,布布扣import java.io.BufferedReader;
bubuko.com,布布扣
import java.io.BufferedWriter;
bubuko.com,布布扣
import java.io.File;
bubuko.com,布布扣
import java.io.FileOutputStream;
bubuko.com,布布扣
import java.io.InputStream;
bubuko.com,布布扣
import java.io.InputStreamReader;
bubuko.com,布布扣
import java.io.OutputStreamWriter;
bubuko.com,布布扣
import java.io.PrintWriter;
bubuko.com,布布扣
import java.net.ServerSocket;
bubuko.com,布布扣
import java.net.Socket;
bubuko.com,布布扣
bubuko.com,布布扣
class CPThread extends Thread{
bubuko.com,布布扣    Socket socket 
= null;
bubuko.com,布布扣    
public CPThread(Socket socket){
bubuko.com,布布扣        
this.socket = socket;
bubuko.com,布布扣    }

bubuko.com,布布扣    
public void run(){
bubuko.com,布布扣        
try{
bubuko.com,布布扣            InputStream in 
= 
bubuko.com,布布扣                      socket.getInputStream();
bubuko.com,布布扣            PrintWriter out 
= 
bubuko.com,布布扣                  
new PrintWriter(
bubuko.com,布布扣                    
new BufferedWriter(
bubuko.com,布布扣                      
new OutputStreamWriter(
bubuko.com,布布扣                        socket.getOutputStream())),
true);
bubuko.com,布布扣            
bubuko.com,布布扣            
while(true)
bubuko.com,布布扣            
{
bubuko.com,布布扣                
//第一个參数为命令
bubuko.com,布布扣
                byte cmd[] = new byte[128];
bubuko.com,布布扣                
int b = 0;
bubuko.com,布布扣                
while(b<cmd.length){
bubuko.com,布布扣                    b 
+= in.read(cmd, b, cmd.length-b);
bubuko.com,布布扣                }

bubuko.com,布布扣                
int ends = 0;
bubuko.com,布布扣                
for(int i=0;i<cmd.length;i++){
bubuko.com,布布扣                    
if(cmd[i]==-0){
bubuko.com,布布扣                        ends 
= i;
bubuko.com,布布扣                        
break;
bubuko.com,布布扣                    }

bubuko.com,布布扣                }

bubuko.com,布布扣                String cmds 
= new String(cmd,0,ends);
bubuko.com,布布扣                
if("exit".equals(cmds)){
bubuko.com,布布扣                    System.exit(
0);
bubuko.com,布布扣                }

bubuko.com,布布扣                
else if("cp".equals(cmds)){
bubuko.com,布布扣                    
byte[] filename = new byte[256];
bubuko.com,布布扣                    b 
= 0;
bubuko.com,布布扣                    
while(b<filename.length){
bubuko.com,布布扣                        b 
+= in.read(filename, b, filename.length-b);
bubuko.com,布布扣                    }

bubuko.com,布布扣                    ends 
= 0;
bubuko.com,布布扣                    
for(int i=0;i<filename.length;i++){
bubuko.com,布布扣                        
if(filename[i]==-0){
bubuko.com,布布扣                            ends 
= i;
bubuko.com,布布扣                            
break;
bubuko.com,布布扣                        }

bubuko.com,布布扣                    }

bubuko.com,布布扣                    String filenames 
= new String(filename,0,ends);
bubuko.com,布布扣                    File fileout 
= new File(filenames);
bubuko.com,布布扣                    
if(fileout.isFile()){
bubuko.com,布布扣                        
throw new Exception("file exists"+fileout.getAbsolutePath());
bubuko.com,布布扣                    }

bubuko.com,布布扣                    FileOutputStream fos 
= new FileOutputStream(fileout);
bubuko.com,布布扣                    
bubuko.com,布布扣                    
byte[] filesize = new byte[64];
bubuko.com,布布扣                    b 
= 0;
bubuko.com,布布扣                    
while(b<filesize.length){
bubuko.com,布布扣                        b 
+= in.read(filesize, b, filesize.length-b);
bubuko.com,布布扣                    }

bubuko.com,布布扣                    
bubuko.com,布布扣                    ends 
= 0;
bubuko.com,布布扣                    
for(int i=0;i<filesize.length;i++){
bubuko.com,布布扣                        
if(filesize[i]==-0){
bubuko.com,布布扣                            ends 
= i;
bubuko.com,布布扣                            
break;
bubuko.com,布布扣                        }

bubuko.com,布布扣                    }

bubuko.com,布布扣                    String filesizes 
= new String(filesize,0,ends);
bubuko.com,布布扣                    
bubuko.com,布布扣                    System.out.println(
"filesize:"+filesizes);
bubuko.com,布布扣                    
int ta = Integer.parseInt(filesizes);
bubuko.com,布布扣                    
byte[] buf = new byte[1024*10];
bubuko.com,布布扣                    
//InputStream ins = socket.getInputStream();
bubuko.com,布布扣
                    while(true){
bubuko.com,布布扣                        
if(ta==0){
bubuko.com,布布扣                            
break;
bubuko.com,布布扣                        }

bubuko.com,布布扣                        
int len = ta;
bubuko.com,布布扣                        
if(len>buf.length){
bubuko.com,布布扣                            len 
= buf.length;
bubuko.com,布布扣                        }

bubuko.com,布布扣                        
int rlen = in.read(buf, 0, len);
bubuko.com,布布扣                        
bubuko.com,布布扣                        
//int rlen = ins.read(buf, 0, len);
bubuko.com,布布扣
                        ta -= rlen;
bubuko.com,布布扣                        
if(rlen>0){
bubuko.com,布布扣                            fos.write(buf,
0,rlen);
bubuko.com,布布扣                            fos.flush();
bubuko.com,布布扣                        }

bubuko.com,布布扣                        
else{
bubuko.com,布布扣                            
break;
bubuko.com,布布扣                        }

bubuko.com,布布扣                    }

bubuko.com,布布扣                    out.println(
"cp finish!");
bubuko.com,布布扣                    fos.close();
bubuko.com,布布扣                    
break;
bubuko.com,布布扣                }

bubuko.com,布布扣                
else{
bubuko.com,布布扣                    System.out.println(
"err command!");
bubuko.com,布布扣                    out.println(
"err command!");
bubuko.com,布布扣                    
break;
bubuko.com,布布扣                }

bubuko.com,布布扣            }

bubuko.com,布布扣            socket.close();
bubuko.com,布布扣        }
catch(Exception e){
bubuko.com,布布扣            e.printStackTrace();
bubuko.com,布布扣            
bubuko.com,布布扣        }

bubuko.com,布布扣    }

bubuko.com,布布扣}

bubuko.com,布布扣
public class JCPServer {
bubuko.com,布布扣
bubuko.com,布布扣    
/**
bubuko.com,布布扣     * 
@param args
bubuko.com,布布扣     
*/

bubuko.com,布布扣    
public static void main(String[] args) throws Exception {
bubuko.com,布布扣        
// TODO Auto-generated method stub
bubuko.com,布布扣
        int num = args.length;
bubuko.com,布布扣        
if(num<=0){
bubuko.com,布布扣            System.out.println(
"please input server port!");
bubuko.com,布布扣            System.exit(
0);
bubuko.com,布布扣        }

bubuko.com,布布扣        
int port = Integer.parseInt(args[0]);
bubuko.com,布布扣        System.out.println(
"you input port:"+port);
bubuko.com,布布扣        
//绑定接受数据端口
bubuko.com,布布扣
        ServerSocket s = new ServerSocket(port);
bubuko.com,布布扣        
while(true){
bubuko.com,布布扣            Socket socket 
= s.accept();
bubuko.com,布布扣            
new CPThread(socket).start();
bubuko.com,布布扣        }

bubuko.com,布布扣    }

bubuko.com,布布扣
bubuko.com,布布扣}

bubuko.com,布布扣

 

client 端

 

bubuko.com,布布扣import java.io.BufferedWriter;
bubuko.com,布布扣
import java.io.File;
bubuko.com,布布扣
import java.io.FileInputStream;
bubuko.com,布布扣
import java.io.OutputStream;
bubuko.com,布布扣
import java.io.OutputStreamWriter;
bubuko.com,布布扣
import java.io.PrintWriter;
bubuko.com,布布扣
import java.net.InetAddress;
bubuko.com,布布扣
import java.net.Socket;
bubuko.com,布布扣
bubuko.com,布布扣
bubuko.com,布布扣
public class JCPClient {
bubuko.com,布布扣
bubuko.com,布布扣    
/**
bubuko.com,布布扣     * 
@param args
bubuko.com,布布扣     
*/

bubuko.com,布布扣    
public static void main(String[] args)throws Exception {
bubuko.com,布布扣        
// TODO Auto-generated method stub
bubuko.com,布布扣
        int num = args.length;
bubuko.com,布布扣        
if(num<=3){
bubuko.com,布布扣            System.out.println(
"please input server ip port filefrom fileto!");
bubuko.com,布布扣            System.exit(
0);
bubuko.com,布布扣        }

bubuko.com,布布扣        System.out.println(
"host:"+args[0]);
bubuko.com,布布扣        System.out.println(
"port:"+args[1]);
bubuko.com,布布扣        System.out.println(
"filefrom:"+args[2]);
bubuko.com,布布扣        System.out.println(
"fileto:"+args[3]);
bubuko.com,布布扣        InetAddress addr 
= InetAddress.getByName(args[0]);
bubuko.com,布布扣        Socket socket 
= 
bubuko.com,布布扣              
new Socket(addr, Integer.parseInt(args[1]));
bubuko.com,布布扣        OutputStream out 
= socket.getOutputStream();
bubuko.com,布布扣        
byte[] cmd = new byte[128];
bubuko.com,布布扣        
byte[] tcmd = "cp".getBytes();
bubuko.com,布布扣        
for(int i=0;i<tcmd.length;i++){
bubuko.com,布布扣            cmd[i] 
= tcmd[i];
bubuko.com,布布扣        }

bubuko.com,布布扣        cmd[tcmd.length] 
= -0;
bubuko.com,布布扣        out.write(cmd,
0,cmd.length);
bubuko.com,布布扣        
//文件名称
bubuko.com,布布扣
        byte[] file = new byte[256];
bubuko.com,布布扣        
byte[] tfile = args[3].getBytes();
bubuko.com,布布扣        
for(int i=0;i<tfile.length;i++){
bubuko.com,布布扣            file[i] 
= tfile[i];
bubuko.com,布布扣        }

bubuko.com,布布扣        file[tfile.length] 
= -0;
bubuko.com,布布扣        out.write(file,
0,file.length);
bubuko.com,布布扣        
bubuko.com,布布扣        
//大小
bubuko.com,布布扣
        File filein = new File(args[2]);
bubuko.com,布布扣        
byte[] size = new byte[64];
bubuko.com,布布扣        
byte[] tsize = (""+filein.length()).getBytes();
bubuko.com,布布扣        
bubuko.com,布布扣        
for(int i=0;i<tsize.length;i++){
bubuko.com,布布扣            size[i] 
= tsize[i];
bubuko.com,布布扣        }

bubuko.com,布布扣        size[tsize.length] 
= -0;
bubuko.com,布布扣        out.write(size,
0,size.length);
bubuko.com,布布扣        
bubuko.com,布布扣        FileInputStream fis 
= null;
bubuko.com,布布扣        
byte[] buf = new byte[1024*10];
bubuko.com,布布扣        
//char[] bufC = new char[1024*10];
bubuko.com,布布扣
        fis = new FileInputStream(filein);
bubuko.com,布布扣        
int readsize = 0;
bubuko.com,布布扣        
//OutputStream ops = socket.getOutputStream();
bubuko.com,布布扣
        while((readsize = fis.read(buf, 0, buf.length))>0){
bubuko.com,布布扣            out.write(buf,
0,readsize);
bubuko.com,布布扣            out.flush();
bubuko.com,布布扣        }

bubuko.com,布布扣        socket.close();
bubuko.com,布布扣    }

bubuko.com,布布扣
bubuko.com,布布扣}

bubuko.com,布布扣

java实现文件传输

标签:style   http   color   io   os   ar   java   for   文件   

原文地址:http://www.cnblogs.com/yxwkf/p/4013955.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!