package com.coffee.taxes.server.socket;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import com.coffee.taxes.server.socket.thread.CreateUpLoadServerThread;
public class UpLoadServer {
/**
* 服务器
*/
private ServerSocket server = null;
/**
* 传输socket
*/
private Socket socket;
/**
* 输入流
*/
private DataInputStream input;
/**
* 输出流
*/
private DataOutputStream output;
/**
* 监听端口
*/
private int port = 9999;
public UpLoadServer(){
try{
server = new ServerSocket(port);
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();
socket.setSoTimeout(0);
CreateUpLoadServerThread c = new CreateUpLoadServerThread(socket);
c.start();
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
new UpLoadServer();
}
}
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.server.logic.net.LoginSocketResponse;
import com.coffee.taxes.server.tools.CryptoClass;
import com.coffee.taxes.server.tools.FileTools;
public class CreateUpLoadServerThread extends Thread {
private Socket socket;
private DataInputStream input;
private DataOutputStream output;
private DataOutputStream fileOutput;
private final int MAXSIZE = 10*1024*1024;
public CreateUpLoadServerThread(Socket s)throws Exception{
try{
this.socket = s;
this.input = new DataInputStream(new BufferedInputStream(this.socket.getInputStream()));
this.output = new DataOutputStream(this.socket.getOutputStream());
}catch(Exception ie){
throw ie;
}
}
public void run() {
try{
System.out.println("有连接进入,开始接受文件!");
//获得文件名
String validateInfo = input.readUTF();
String [] validateInfos = validateInfo.split(",");
String fileName = validateInfos[0];
String userName = validateInfos[1];
String passWord = validateInfos[2];
//文件长度
Long len = input.readLong();
//用户名解密
userName=CryptoClass.encryptionStr(userName, userName.length());
userName=CryptoClass.conversionArrayToStr(userName);
System.out.println("文件名:"+fileName);
System.out.println("用户名:"+userName);
System.out.println("密码:"+passWord);
System.out.println("文件长度:"+len);
LoginSocketResponse login = new LoginSocketResponse();
//判断上传文件是否大于10M
if(this.MAXSIZE > len && login.upLoadLoginConfirm(userName, passWord)){
output.writeUTF("1");
output.flush();
//获取当前路径
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 filePath = folderPath + "//" + fileName;
this.fileOutput = new DataOutputStream(new BufferedOutputStream(new BufferedOutputStream(new FileOutputStream(filePath))));
System.out.println("开始接受文件");
//定义缓存
int bufferSize = 8192;
byte [] buf = new byte[bufferSize];
//已传输大小
double passedlen = 0;
int read = 0;
if(input != null){
while((read=input.read(buf))!=-1){
passedlen += read;
fileOutput.write(buf, 0, read);
if(passedlen==read){
break;
}
}
}
output.writeUTF("1");
output.flush();
}else{
output.writeUTF("0");
output.flush();
System.out.println("文件大于10M,或者用户名密码错误");
}
String lastRequest = input.readUTF();
if(lastRequest != null && "end".equals(lastRequest)){
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();
}
}
}
package com.coffee.taxes.server.socket;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.net.Socket;
import com.coffee.taxes.bean.db.TaxesUser;
import com.coffee.taxes.server.socket.thread.CreateUpLoadClientThread;
public class UpLoadClient {
private Socket socket;
private String ip ;
private int port ;
private DataInputStream input;
private DataOutputStream output;
private DataInputStream fileInput;
public UpLoadClient(String ip , int port){
this.ip = ip;
this.port = port;
}
public void sendFile(File file, TaxesUser taxesUser){
try{
CreateUpLoadClientThread c= new CreateUpLoadClientThread(file,ip,port,taxesUser);
c.start();
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
UpLoadClient c = new UpLoadClient("192.168.1.203",9999);
// File f = new File("E://WORK_PROJECT//Accounters0307.rar");
File f = new File("E://Probject//ManageStudent.rar");
// File f = new File("E://SOFT//DTLite4355-0068.zip");
TaxesUser taxesUser = new TaxesUser();
taxesUser.setT_userName("admin");
taxesUser.setT_userPwd("83nq88gxsE3hU0adG+w0Xg==");
c.sendFile(f,taxesUser);
}
}
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.bean.db.TaxesUser;
import com.coffee.taxes.server.tools.CryptoClass;
public class CreateUpLoadClientThread extends Thread {
private Socket socket;
private DataInputStream input ;
private DataOutputStream output;
private DataInputStream fileInput;
private File file;
private String ip ;
private int port;
private final int MAXSIZE = 10*1024*1024;
public CreateUpLoadClientThread(File file,String ip , int port,TaxesUser taxesUser){
try{
if(file.length()>this.MAXSIZE){
System.out.println("上传错误,文件大于10M!");
return ;
}
//建立通信连接
socket = new Socket(ip,port);
socket.setSoTimeout(0);
//创建传输流
input = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
output = new DataOutputStream(socket.getOutputStream());
//创建读取文件流
fileInput = new DataInputStream(new BufferedInputStream(new BufferedInputStream(new FileInputStream(file))));
//获得文件名、文件大小、用户名、密码
String fileName = file.getName();
Long len = file.length();
String userName = taxesUser.getT_userName();
String passWord = taxesUser.getT_userPwd();
//用户名进行加密
userName=CryptoClass.conversionStrToArray(userName);
userName=CryptoClass.encryptionStr(userName, userName.length());
output.writeUTF(fileName+","+userName+","+passWord);
//写入文件名和文件大小
output.flush();
output.writeLong(len);
output.flush();
/**
* 接收服务器端验证信息 0 为失败 1为成功
*/
String isValidate=input.readUTF();
if(null==isValidate || isValidate.equals("0")){
this.socket.close();
return;
}
//定义缓存
int bufferSize = 8192;
byte[] buf = new byte[bufferSize];
//已传输大小
double passedlen = 0;
int read = 0;
if(fileInput!=null){
while((read = fileInput.read(buf))!=-1){
passedlen+=read;
System.out.println("文件接收了" + (int)(((double)passedlen/(double)file.length())*100) + "%/n");
output.write(buf,0,read);
}
}
try{
output.flush();
}catch(Exception e){
e.printStackTrace();
}
//接收传输结果装填
isValidate = input.readUTF();
if(isValidate!=null){
output.writeUTF("end");
this.closeSocket();
}
//判断传输返回结果
if("0".equals(isValidate)){
System.out.println("上传失败!");
}else if("1".equals(isValidate)){
System.out.println("上传成功 !");
}
}catch(Exception e){
e.printStackTrace();
}finally{
this.closeSocket();
}
}
/**
* 关闭连接
*/
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();
}
}
}