Socket 上传/下载文件

来源:互联网 发布:数据加密方式有哪些 编辑:程序博客网 时间:2024/05/07 10:29

package com.coffee.taxes.server.socket;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import com.coffee.taxes.server.socket.thread.CreateDownLoadServerThread;

public class DownLoadServer {
 /**
  * 服务器
  */
 private ServerSocket server = null;
 /**
  * 传输socket
  */
 private Socket socket;
 /**
  * 输入流
  */
 private DataInputStream input;
 /**
  * 输出流
  */
 private DataOutputStream output;
 /**
  * 监听端口
  */
 private int port = 9999;
 /**
  * 文件写入流
  */
 private DataInputStream fileInput;
 /**
  * 文件信息
  */
 private File file;
 
 
 public DownLoadServer(File file){
  try{
   server = new ServerSocket(9999);
   this.file = file;
   while(true){
    init();
   }
  }catch(Exception e){
   if(!server.isClosed()){
    try {
     server.close();
    } catch (IOException e1) {
     System.out.println("关闭服务器连接错误");
     e1.printStackTrace();
    }
   }
   System.out.println("创建服务器连接错误");
   e.printStackTrace();
  }
 }
 
 /**
  * 开始执行文件连接
  */
 public void init(){
  try{
   System.out.println("等待客户端连接!");
   socket = server.accept();
   CreateDownLoadServerThread c = new CreateDownLoadServerThread(socket,this.file);
   c.start();
  }catch(Exception e){
   e.printStackTrace();
  }
 }
 
 
 public static void main(String[] args) {
  new DownLoadServer(new File("E://SOFT//MyEclipse_5.5GA_E3.2.2_FullStackInstaller.exe"));
 }
}

package com.coffee.taxes.server.socket.thread;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.Socket;

import com.coffee.taxes.server.logic.net.LoginSocketResponse;
import com.coffee.taxes.server.tools.CryptoClass;

public class CreateDownLoadServerThread  extends Thread  {
 private Socket socket;
 private DataInputStream input;
 private DataOutputStream output;
 private DataInputStream fileInput;
 private File file;
 
 public CreateDownLoadServerThread(Socket s, File file){
  try{
   //判断是否是用户
   this.socket = s;
   this.file = file;
   input = new DataInputStream(new BufferedInputStream(this.socket.getInputStream()));
   output = new DataOutputStream(this.socket.getOutputStream());
  }catch(Exception e){
   e.printStackTrace();
  }
 }

 public void run() {
  try{
   System.out.println("有连接进入,开始传输文件!");
   
   //创建输入输出流
   input = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
   output = new DataOutputStream(socket.getOutputStream());
   
   //获取用户名和密码
   String userName = input.readUTF();
   String passWord = input.readUTF();
   
   //用户名解密
   userName=CryptoClass.encryptionStr(userName, userName.length());
   userName=CryptoClass.conversionArrayToStr(userName);
   
   System.out.println("用户名:"+userName);
   System.out.println("密码:"+passWord);
   
   LoginSocketResponse login = new LoginSocketResponse();
   
   if(login.upLoadLoginConfirm(userName, passWord)){
    output.writeUTF("1");
    output.flush();
    //输出文件名和文件长度
    String fileName = this.file.getName();
    Long len = this.file.length();
    output.writeUTF(fileName);
    output.flush();
    output.writeLong(len);
    output.flush();
    
    //文件输出流
    fileInput = new DataInputStream(new BufferedInputStream(new BufferedInputStream(new FileInputStream(this.file))));
    
    System.out.println("开始传输文件");
    
    //定义缓存
    int bufferSize = 8192;
    byte [] buf = new byte[bufferSize];
    
    int read = 0;
    if(fileInput != null){
     while((read=fileInput.read(buf))!=-1){
      output.write(buf, 0, read);
     }
    }
   }else{
    output.writeUTF("0");
    output.flush();
   }
   //获得结束标志 关闭通信
   String lastRequest = input.readUTF();
   if(lastRequest!=null && "end".equals(lastRequest)){
    System.out.println("关闭通信");
    this.closeSocket();
   }
  }catch(Exception e){
   e.printStackTrace();
  }
 }
 
 /**
  * 关闭连接
  */
 public void closeSocket(){
  try{
   if(this.fileInput!=null){
    this.fileInput.close();
   }
   if(this.input!=null){
    this.input.close();
   }
   if(this.output!=null){
    this.output.close();
   }
   if(this.socket!=null){
    this.socket.close();
   }
  }catch(Exception e){
   e.printStackTrace();
  }
 }
}

 

package com.coffee.taxes.server.socket;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;

import com.coffee.taxes.bean.db.TaxesUser;
import com.coffee.taxes.server.socket.thread.CreateDownLoadClientThread;

public class DownLoadClient {
 private Socket socket;
 private String ip ;
 private int port ;
 private DataInputStream input;
 private DataOutputStream output;
 private DataOutputStream fileOutput;
 
 public DownLoadClient(String ip , int port){
  this.ip = ip;
  this.port = port;
 }
 
 public void sendFile(TaxesUser taxesUser){
  try{
   CreateDownLoadClientThread c = new CreateDownLoadClientThread(ip,port,taxesUser);
  }catch(Exception e){
   e.printStackTrace();
  }
 }
 
 public static void main(String[] args) {
  DownLoadClient c = new DownLoadClient("192.168.1.203",9999);
  TaxesUser taxesUser = new TaxesUser();
  taxesUser.setT_userName("admin");
  taxesUser.setT_userPwd("83nq88gxsE3hU0adG+w0Xg==");
  c.sendFile(taxesUser);
 }
}

 

 

package com.coffee.taxes.server.socket.thread;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.Socket;
import java.util.Date;

import com.coffee.taxes.bean.db.TaxesUser;
import com.coffee.taxes.server.tools.CryptoClass;
import com.coffee.taxes.server.tools.FileTools;


public class CreateDownLoadClientThread  extends Thread  {
 private Socket socket;
 private String ip ;
 private int port ;
 private DataInputStream input;
 private DataOutputStream output;
 private DataOutputStream fileOutput;
 
 public CreateDownLoadClientThread(String ip,int port,TaxesUser taxesUser){
  try{
   //建立通信连接
   socket = new Socket(ip,port);
   
   //创建传输流
   input = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
   output = new DataOutputStream(socket.getOutputStream());
   
   //得到用户名和密码
   String userName = taxesUser.getT_userName();
   String passWord = taxesUser.getT_userPwd();
   
   //用户名进行加密
   userName=CryptoClass.conversionStrToArray(userName);
   userName=CryptoClass.encryptionStr(userName, userName.length());
   
   //写入用户名和密码
   output.writeUTF(userName);
   output.flush();
   output.writeUTF(passWord);
   output.flush();
   
   //获取返回值
   String isValidate = input.readUTF();
   System.out.println(isValidate);
   //判断用户名和密码是否正确
   if(isValidate==null || "0".equals(isValidate)){
    this.closeSocket();
    return ;
   }
   
   //获取当前路径
   File directory = new File("");
   //获得当前工程根目录
   String folderPath = directory.getCanonicalPath();
   //创建文件夹路径
   folderPath += "//"+FileTools.getStringByDate(new Date(),"yyyy//MM//dd");
   
   System.out.println("folderPath:"+folderPath);
   
   //创建文件夹
   FileTools.createFolder(folderPath);
   
   //获得文件名
   String fileName = input.readUTF();
   String filePath = folderPath + "//" + fileName;
   //文件长度
   Long len = input.readLong();
   System.out.println("文件名:"+fileName);
   System.out.println("文件长度:"+len);
   
   //创建读取文件流
   fileOutput = new DataOutputStream(new BufferedOutputStream(new BufferedOutputStream(new FileOutputStream(filePath))));
   
   
   //定义缓存
   int bufferSize = 8192;
            byte[] buf = new byte[bufferSize];
   
            //已传输大小
   double passedlen = 0;
           
   int read = 0;
   
   if(fileOutput!=null){
    while((read = input.read(buf))!=-1){
     passedlen += read;
     System.out.println("文件接收了" +  (int)(((double)passedlen/(double)len)*100) + "%/n");
     fileOutput.write(buf,0,read);
     //传输结束跳出循环
     if(passedlen==len){
      break;
     }
    }
   }
   fileOutput.flush();
   
   //返回结束标志
   output.writeUTF("end");
   output.flush();
   System.out.println("已发送关闭标识,关闭连接");
   this.closeSocket();
   
  }catch(Exception e){
   e.printStackTrace();
  }
 }
 
 /**
  * 关闭连接
  */
 public void closeSocket(){
  try{
   if(this.fileOutput!=null){
    this.fileOutput.close();
   }
   if(this.input!=null){
    this.input.close();
   }
   if(this.output!=null){
    this.output.close();
   }
   if(this.socket!=null){
    this.socket.close();
   }
  }catch(Exception e){
   e.printStackTrace();
  }
 }
}

 

 

 

原创粉丝点击