您的位置:首页 > 编程语言 > Java开发

Java代码实现两台电脑之间传文件(3)

2017-10-27 15:22 471 查看
这里是在原来传文件、图片的基础之上增加了一些新功能还有修正了一些BUG

不建议各位围观这里的代码

初学者比较合适看我前面那边小博客http://blog.csdn.net/liumang9438/article/details/78322531

可谓短小精悍、触类旁通、八面玲珑、六……咳咳,有点过分了

总之呢

别看这一篇就OK了

悄悄告诉大噶

这只是我心血来潮时瞎写的东西

不忍心删咯

才把它放在这里[斜眼猥琐笑]

实例代码:

//父类Transfer.java
import java.io.*;
import java.net.Socket;

public class Transfer {
private File sendFile;
private File saveFile;
private FileInputStream in;
private FileOutputStream out;
private FileInputStream sendStream;
private FileOutputStream saveStream;
private Socket socket;
private int port;

public Transfer() {
sendFile = new File("C:\\xiaoxiao\\tempSend.txt");   //默认发送的文件
saveFile = new File("C:\\xiaoxiao\\tempSave.txt");   //默认存文件的文件
port = 2017;        //默认端口
}

public void init() {
try {
if (!sendFile.exists()) {       //防止报错
/*getParentsFile()是否为空,如果不为空,则需要创建父级即所需文件夹
*2017年10月25日由陈舒毅发现并改正的bug
*/
if (null != sendFile.getParent()) {
sendFile.getParentFile().mkdirs();
}
sendFile.createNewFile();
}
if (!saveFile.exists()) {
/*getParentsFile()是否为空,如果不为空,则需要创建父级即所需文件夹
*2017年10月25日由陈舒毅发现并改正的bug
*/
if (null != saveFile.getParent()) {
saveFile.getParentFile().mkdirs();
}
saveFile.createNewFile();
}
sendStream = new FileInputStream(sendFile);
saveStream = new FileOutputStream(saveFile);
} catch(Exception e) {
System.out.println(e);
}
}

//发送默认文件或者用setter指定的文件
public void send() {
try {
init();     //这是是防止使用setter函数之后,及时更新数据
byte[] b = new byte[64];
int n = sendStream.read(b);
while (-1 != n) {
out.write(b, 0, n);
n = sendStream.read(b);
}
} catch (IOException e) {
System.out.println(e);
}
}

//发送指定文件
public void send(File file) {
try {
sendFile = file;
init();     //这是是防止使用setter函数之后,及时更新数据
byte[] b = new byte[64];
int n = sendStream.read(b);
while (-1 != n) {
out.write(b, 0, n);
n = sendStream.read(b);
}
} catch (IOException e) {
System.out.println(e);
}
}

//用DataOutputStream发送指定字符串
public void send(String sendStr) {
try {
DataOutputStream output = new DataOutputStream(socket.getOutputStream());
output.writeUTF(sendStr);
output.close();
} catch (Exception e) {
System.out.println(e);
}
}

public void receive() {     //以字节流的方式接收文件
try {
init();     //这是是防止使用setter函数之后,及时更新数据
byte[] b = new byte[64];
int n = in.read(b);
while (-1 != n) {
saveStream.write(b, 0, n);
n = in.read(b);
}
} catch (Exception e) {
System.out.println(e);
}
}

//将接收的文件放在指定的位置
public void receive(File savePathName) {
try {
saveFile = savePathName;
init();     //初始化
byte[] b = new byte[64];
int n = in.read(b);
while (-1 != n) {
saveStream.write(b, 0, n);
n = in.read(b);
}
} catch (Exception e) {
System.out.println(e);
}
}

//接收从DataOutputStream传过来的字符串
public String receiveString() {
try {
DataInputStream input = new DataInputStream(socket.getInputStream());
String str =  input.readUTF();
input.close();
return str;
} catch (Exception e) {
System.out.println(e);
}
return "";
}

public void closeAll() {        //关闭全部流和套接字
try {
if (null != sendStream) {
sendStream.close();
}
if (null != out) {
out.close();
}
if (null != saveStream) {
saveStream.close();
}
if (null != in) {
in.close();
}
if (null != socket) {
socket.close();
}
} catch (Exception e) {
System.out.println(e);
}
}

//Setter和Getter函数
public File getSendFile() {
return sendFile;
}

public void setSendFile(File sendFile) {
this.sendFile = sendFile;
}

public File getSaveFile() {
return saveFile;
}

public void setSaveFile(File saveFile) {
this.saveFile = saveFile;
}

public int getPort() {
return port;
}

public void setPort(int port) {
this.port = port;
}

public Socket getSocket() {
return socket;
}

public void setSocket(Socket socket) {
try {
if (null != this.socket) {
this.socket.close();
}
this.socket = socket;   //初始化套接字
//输入输出的初始化
in  = (FileInputStream)socket.getInputStream();
out = (FileOutputStream)socket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
}
}

//子类服务器TransferServer.java
import java.io.*;
import java.net.Socket;
import java.net.ServerSocket;

public class TransferServer extends Transfer{
private ServerSocket server;

public TransferServer() {
super();
setSendFile(new File("C:\\xiaoxiao\\tempServerSend.txt"));
setSaveFile(new File("C:\\xiaoxiao\\tempServerSave.txt"));
}

@Override
public void init() {
try {
super.init();
if (null != server) {
server.close();
}
server = new ServerSocket(getPort());
} catch(Exception e) {
System.out.println(e);
}
}

//等待连接
public void waitConnect() {
System.out.println("服务器已经启动...");
try {
init();     //初始化
Socket tmpS = server.accept();  //阻塞,等待连接
setSocket(tmpS);
} catch (IOException e) {
System.out.println(e);
}
}

@Override
public void setPort(int port) {
super.setPort(port);
try {
if (null != server) {
server.close();
}
server = new ServerSocket(port);
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public void closeAll() {
super.closeAll();
try {
if (null != server) {
server.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

//测试程序
public static void main(String[] args) {
TransferServer server = new TransferServer();
server.waitConnect();
server.send(new File("E:\\shaohan.png"));
server.closeAll();
}
}

//子类客户端类TransferClient.java
import java.io.*;
import java.net.Socket;

public class TransferClient extends Transfer {
private String host;

public TransferClient() {
super();
host = "127.0.0.1";     //默认连接的地址主机
setSendFile(new File("C:\\xiaoxiao\\tempClientSend.txt"));
setSaveFile(new File("C:\\xiaoxiao\\tempClientSave.txt"));
}

public void connect() {
System.out.println("客户端已经启动...");
try {
setSocket(new Socket(host, getPort()));
} catch (IOException e) {
e.printStackTrace();
}
}

public void connect(String host, int port) {
System.out.println("客户端已经启动...");
try {
this.host = host;
setPort(port);
setSocket(new Socket(host, port));
} catch (IOException e) {
e.printStackTrace();
}
}

public String getHost() {
return host;
}

public void setHost(String host) {
this.host = host;
}

//测试函数
public static void main(String[] args) {
TransferClient client = new TransferClient();
client.connect();
client.receive(new File("D:\\image\\shaohan.png"));
client.closeAll();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java socket 电脑 传文件