怎样才能通过TCP传输协议用Java实现文件的传输?

slowside45 2017-05-24 01:03:49
问题背景:

正在学习Java编程,在学习TCP传输协议的时候遇到了问题。

Receive端有一个名为prepare.txt的空文档,用来接收数据。

Sender端有一个名为data.txt的有内容的文档,用来传输数据。

我希望在本机上完成文件的传输。

Receive端代码:


public class pro1Receiver {
public static void main(String args[]){
try {
ServerSocket ss = new ServerSocket(9999);
Socket s = ss.accept();
String pathName = "D:\\prepare.txt"; //空文本,用来接收数据。
File file = createFile(pathName);
InputStream in = s.getInputStream();
FileOutputStream fos = new FileOutputStream(file);
System.out.println("开始接数据...");
byte[] buffer = new byte[1024];
int len = 0;
int dataCounter = 0;
while((len=in.read(buffer))!=-1){
fos.write(buffer, 0, len);
dataCounter+=len;
}
fos.close();
in.close();
s.close();
ss.close();
System.out.println("数据传输完毕,大小为" + dataCounter + "字节");
} catch (IOException e) {
e.printStackTrace();
}
}
public static File createFile(String pathName){
File file = new File(pathName);
if(file.exists()&&file.isFile()){
System.out.println("使用已存在的文件");
}
else{
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
return file;
}
}



Sender端代码:

public class pro2Sender {
public static void main(String[] args){
String pathName = "D:\\data.txt"; \\有内容的文本,期望将内容传输到 Receive端用来接收数据的文件里
File file = new File(pathName);
byte[] IP = new byte[]{127,0,0,1};
if (!(findFile(pathName))){
return;
}
System.out.println("正在连接到目标主机……");
try{
System.out.println("正在连接到目标主机...");
InetAddress addr = InetAddress.getByAddress(new byte[] { 127, 0, 0,1 });
Socket s = new Socket(addr, 9999);
OutputStream output = s.getOutputStream();
FileInputStream fis = new FileInputStream(sourceFile);
System.out.println("连接成功,正在传输数据...");
byte[] buffer = new byte[1024];
int len = 0;
int dataCounter = 0;
while ((len = fis.read(buffer)) != -1) {
output.write(buffer, 0, len);
dataCounter += len;
}
fis.close();
output.close();
s.close();
System.out.println("数据传送完毕,共传送" + dataCounter + "字节。");
}catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static boolean findFile(String pathName){
File file = new File(pathName);
if(file.isFile())
return true;
else return false;
}
}

错误信息:

先运行Receiver端,然后运行Sender端,console显示传输了0字节的内容

看起来已经成功建立连接,但在文件传输的时候出现了错误,准备好的data.txt中的内容没有传输到prepare.txt中。

望前辈们不吝赐教。
...全文
938 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
slowside45 2017-05-24
  • 打赏
  • 举报
回复
引用 1 楼 bcsflilong 的回复:
output

关闭前  你是不是应该output.flush(); 操作一下呢
没错!加了flush以后就传输成功了。 但是Receive端代码里的CreateFile方法无法被调用了,只能提前创建好txt文件,这是怎么回事呢
爱摸鱼de老邪 2017-05-24
  • 打赏
  • 举报
回复
结果 正在连接到目标主机…… 正在连接到目标主机... 连接成功,正在传输数据... 数据传送完毕,共传送379440字节。
爱摸鱼de老邪 2017-05-24
  • 打赏
  • 举报
回复
发送端

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class pro2Sender {
	public static void main(String[] args) {
		String pathName = "D:\\data.txt";
		File file = new File(pathName);
		byte[] IP = new byte[] { 127, 0, 0, 1 };
		if (!(findFile(pathName))) {
			return;
		}
		System.out.println("正在连接到目标主机……");
		try {
			System.out.println("正在连接到目标主机...");
			InetAddress addr = InetAddress.getByAddress(IP);
			Socket s = new Socket(addr, 9999);
			OutputStream output = s.getOutputStream();
			FileInputStream fis = new FileInputStream(file);
			System.out.println("连接成功,正在传输数据...");
			byte[] buffer = new byte[1024];
			int len = 0;
			int dataCounter = 0;
			while ((len = fis.read(buffer)) != -1) {
				output.write(buffer, 0, len);
				dataCounter += len;
			}
			fis.close();
			output.close();
			s.close();
			System.out.println("数据传送完毕,共传送" + dataCounter + "字节。");
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static boolean findFile(String pathName) {
		File file = new File(pathName);
		if (file.isFile())
			return true;
		else
			return false;
	}
}
接收端

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class pro1Receiver {
	public static void main(String args[]) {
		try {
			ServerSocket ss = new ServerSocket(9999);
			Socket s = ss.accept();
			String pathName = "D:\\prepare.txt"; // 空文本,用来接收数据。
			File file = pro1Receiver.createFile(pathName);
			InputStream in = s.getInputStream();
			FileOutputStream fos = new FileOutputStream(file);
			System.out.println("开始接数据...");
			byte[] buffer = new byte[1024];
			int len = 0;
			int dataCounter = 0;
			while ((len = in.read(buffer)) != -1) {
				fos.write(buffer, 0, len);
				dataCounter += len;
			}
			fos.close();
			in.close();
			s.close();
			ss.close();
			System.out.println("数据传输完毕,大小为" + dataCounter + "字节");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static File createFile(String pathName) {
		File file = new File(pathName);
		if (file.exists() && file.isFile()) {
			System.out.println("使用已存在的文件");
		} else {
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return file;
	}
}
bcsflilong 2017-05-24
  • 打赏
  • 举报
回复
output

关闭前  你是不是应该output.flush(); 操作一下呢

51,411

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧