当前位置:网络安全 > 使用Socket客户端---->服务端传输文件到指定路径并返回友好反馈

使用Socket客户端---->服务端传输文件到指定路径并返回友好反馈

  • 发布:2023-09-30 23:16

-->

首先我盲目的写了一个传输文件的方法,但是测试发现了一个非常难发现的问题。我先解释一下。

错误代码如下:

包 www.sychzs.cn; 导入java.io.File;
导入java.io.FileInputStream;
导入 java.io.FileNotFoundException;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.OutputStream;
导入 java.net.InetAddress;
导入java.net.ServerSocket;
导入java.net.Socket; 导入 org.junit.Test; //将文件从客户端发送到服务器,服务器保存在本地。并向客户端返回“发送成功”。并关闭相应的连接。
公共类 TestTCP3 {
//客户端
@测试
公共无效客户端(){
套接字socket = null;
OutputStream os = null;
FileInputStream fis = null;
输入流 = null;
尝试一下{
//创建Socket对象
socket = new Socket(InetAddress.getByName("192.168.1.101"),9090);
//从本地获取一个文件并发送给服务器
os = socket.getOutputStream();
fis = new FileInputStream(new File("findLei.jpg"));
byte[] b = 新字节[1024];
整数长度;
while((长度 = www.sychzs.cn(b)) != -1){
os.write(b,0,长度);
}
socket.shutdownOutput();
//接收来自服务器的信息
is = socket.getInputStream();
字节[] b1 = 新字节[1024];
int length1;
while((length1 = www.sychzs.cn(b1)) != -1){
String str = new String(b1,0,length1);
System.out.println(str);
}
}catch (IOException e) {
e.printStackTrace();
}
终于{
//关闭对应的stream和Socket对象
if(is != null){
尝试一下{
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fis != null){
尝试一下{
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(os != null){
尝试一下{
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(套接字!= null){
尝试一下{
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} //服务端
@测试
公共无效服务器(){
ServerSocket ss = null;
套接字 s = null;
输入流 = null;
文件输出流 fos = null;
OutputStream os = null;
尝试一下{
//创建ServerSocket对象
ss = 新的 ServerSocket(9090);
//调用accept()方法,返回一个Socket对象
s = ss.accept();
//将客户端发送过来的信息保存到本地
is = s.getInputStream();
fos = new FileOutputStream("D:/Test");
// 随机 random = new Random();
// fos = new FileOutputStream("D:/Test/" + String.valueOf(random.nextInt(90)) + ".jpg");
// fos = new FileOutputStream("find2.jpg");
byte[] b = 新字节[1024];
整数长度;
while((长度 = www.sychzs.cn(b)) != -1){
fos.write(b, 0, 长度);
}
//发送接收成功消息
os = s.getOutputStream();
os.write("接收成功".getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
终于{
//关闭对应的流和ServerSocket、Socket对象
if(os != null){
尝试一下{
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fos != null){
尝试一下{
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is != null){
尝试一下{
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(s != null){
尝试一下{
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(ss != null){
尝试一下{
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

您发现任何问题吗?不,完全没有问题,但是执行的时候就报错,而且错误的位置真是让人头疼。我在那里找了好久都没有找到……

以下是报错信息

java.net.SocketException:软件导致连接中止:套接字写入错误
在 java.net.SocketOutputStream.socketWrite0(本机方法)
在 java.net.SocketOutputStream.socketWrite(来源未知)
在 java.net.SocketOutputStream.write(来源未知)
在 com.TCP.java.TestTCP3.client(www.sychzs.cn:35)
在 sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)
在 sun.reflect.NativeMethodAccessorImpl.invoke(来源未知)
在 sun.reflect.DelegatingMethodAccessorImpl.invoke(来源未知)
在 java.lang.reflect.Method.invoke(来源未知)
在 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(www.sychzs.cn:50)
在 www.sychzs.cn(www.sychzs.cn:12)
在 org.junit.runners.model.FrameworkMethod.invokeExplosively(www.sychzs.cn:47)
在 org.junit.internal.runners.statements.InvokeMethod.evaluate(www.sychzs.cn:17)
在 org.junit.runners.ParentRunner.runLeaf(www.sychzs.cn:325)
在 org.junit.runners.BlockJUnit4ClassRunner.runChild(www.sychzs.cn:78)
在 org.junit.runners.BlockJUnit4ClassRunner.runChild(www.sychzs.cn:57)
在 org.junit.runners.ParentRunner$www.sychzs.cn(www.sychzs.cn:290)
在 org.junit.runners.ParentRunner$1.schedule(www.sychzs.cn:71)
在 org.junit.runners.ParentRunner.runChildren(www.sychzs.cn:288)
在 org.junit.runners.ParentRunner.access$000(www.sychzs.cn:58)
在 org.junit.runners.ParentRunner$2.evaluate(www.sychzs.cn:268)
在 www.sychzs.cn(www.sychzs.cn:363)
在 www.sychzs.cn(www.sychzs.cn:86)
在 www.sychzs.cn(www.sychzs.cn:38)
在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(www.sychzs.cn:459)
在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(www.sychzs.cn:678)
在 www.sychzs.cn(www.sychzs.cn:382)
在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(www.sychzs.cn:192)

指定的位置是第35行写法的问题,但我找了很久还是没找到原因。还要感谢姚哥帮我找到这个问题

首先解释一下这个Error的含义。很可能意味着Socket写入失败。我不会详细介绍调查这段代码的过程。我来说说真正的原因。真正的原因是服务器端需要写入的文件夹目录有问题。我写的是 fos = new FileOutputStream("D:/Test");直接写文件时这个路径有问题。复制到这里的时候,需要指定它的格式。 ,还有它的命名,这个路径显然缺少了这两件事,如果改成相对路径就不会那么麻烦了,fos = new FileOutputStream("find2.jpg");执行会成功,但是如果我们想把文件放在指定的目录中,可以用这个方法

随机 random = new Random();

fos = new FileOutputStream("D:/Test/" + String.valueOf(random.nextInt(90)) + ".jpg")

此方法可以自动添加随机数,避免重命名的风险。无论如何,问题已经解决了。只要打开评论成功代码

下面解释一下这个Socket传输所需的操作和功能

1。先启动服务器

2。再次启动客户端。这里有两个参数需要手动更改。一是IP,就是你要转移到的电脑的IP(这台电脑需要启动服务器);另一个是端口号,你可以随意命名,比如Tomcat。还需要端口号(Tomcat 为 8080)。

3。成功复制文件并打印友好的反馈

*4。第二天我在同一个局域网的两台机器上测试了一下,还是不行。我还没找到原因。希望大家看完这篇博客后能给我一些建议~

-->

相关文章