首先盲写的一个传输文件的方法,但测试发现了一个非常不容易发现的问题,这里先说明一下。

错误的代码如下:

 package com.TCP.java;

 import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket; import org.junit.Test; //从客户端发送文件给服务端,服务端保存到本地。并返回“发送成功”给客户端。并关闭相应的连接。
public class TestTCP3 {
//客户端
@Test
public void client(){
Socket socket = null;
OutputStream os = null;
FileInputStream fis = null;
InputStream is = null;
try {
//创建一个Socket的对象
socket = new Socket(InetAddress.getByName("192.168.1.101"),9090);
//从本地获取一个文件发送给服务端
os = socket.getOutputStream();
fis = new FileInputStream(new File("findLei.jpg"));
byte[] b = new byte[1024];
int length;
while((length = fis.read(b)) != -1){
os.write(b,0,length);
}
socket.shutdownOutput();
//接收来自服务端的信息
is = socket.getInputStream();
byte[] b1 = new byte[1024];
int length1;
while((length1 = is.read(b1)) != -1){
String str = new String(b1,0,length1);
System.out.println(str);
}
}catch (IOException e) {
e.printStackTrace();
}
finally{
//关闭相应的流和Socket对象
if(is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} //服务端
@Test
public void server(){
ServerSocket ss = null;
Socket s = null;
InputStream is = null;
FileOutputStream fos = null;
OutputStream os = null;
try {
//创建一个ServerSocket的对象
ss = new ServerSocket(9090);
//调用accept()方法,返回一个Socket对象
s = ss.accept();
//将从客户端发送的信息保存到本地
is = s.getInputStream();
fos = new FileOutputStream("D:/Test");
// Random random = new Random();
// fos = new FileOutputStream("D:/Test/" + String.valueOf(random.nextInt(90)) + ".jpg");
// fos = new FileOutputStream("find2.jpg");
byte[] b = new byte[1024];
int length;
while((length = is.read(b)) != -1){
fos.write(b, 0, length);
}
//发送接收成功的消息
os = s.getOutputStream();
os.write("接收成功".getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally{
//关闭相应的流及ServerSocket,Socket对象
if(os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(s != null){
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(ss != null){
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

有看出来问题么,没有,看着一点问题也没有,但执行的时候就是报错,而且报错的位置着实很头痛,在那附近找了很久也没找到......

下面是所报Error的信息

 java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at com.TCP.java.TestTCP3.client(TestTCP3.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

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

先说明一下这个Error的意思,大概就是Socket在写入的时候出现故障了,调查这段代码的过程就不一一详述了,就说一下真正的原因在哪,真正的原因是server端需要写入的文件夹目录有问题,我写的是fos = new FileOutputStream("D:/Test");这个路径在直接写入文件的时候有问题,这里进行复制的时候需要指定其格式,及其命名,这个路径明显缺少这两样,而如果改成相对路径就没有那么麻烦了,fos = new FileOutputStream("find2.jpg");这样执行就成功了,但如果我们想将文件放在指定目录下呢,可以用这种方式

Random random = new Random();

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

这种方式可以自动添加一个随机数,使其避免了重命名的风险,反正问题是已经解决了,成功代码只需打开注释即可

下面说明一下此Socket传输需要进行的操作及实现的功能

1.先开启服务端

2.再启动客户端。其中这里有两个参数是需要手动改的,一个是IP,即你想要传输到的电脑(这台电脑要启动server)的IP;另一个就是端口号,这个是自己随便命名的,像Tomcat一样也需要一个端口号(Tomcat是8080)。

3.成功复制文件并打印友好回馈

*4.第二天在同一局域网上的两台机器上测试,竟然不好使,尚未未找到原因,看到这篇博客的希望给点儿建议~

利用TCP 客户端---->服务端 传送文件到指定路径,并返回一个友好的回馈的更多相关文章

  1. 利用Socket 客户端---->服务端 传送文件到指定路径,并返回一个友好的回馈

    首先盲写的一个传输文件的方法,但测试发现了一个非常不容易发现的问题,这里先说明一下. 错误的代码如下: package com.TCP.java; import java.io.File; impor ...

  2. UDP广播 与 TCP客户端 --服务端

    随着倒计时的响声,自觉无心工作,只想为祖国庆生. 最近有遇到过这样一个问题,将摄像头识别的行人,车辆实时显示在客户端中.有提供接口,会以Json的数据的形式将实时将识别的对象进行Post提交.所以我们 ...

  3. TCP客户端 服务端详细代码

    本文章转自http://www.myexception.cn/program/1912019.html TCP网络编程中connect().listen()和accept()三者之间的关系 基于 TC ...

  4. TCP/IP网络编程之基于TCP的服务端/客户端(一)

    理解TCP和UDP 根据数据传输方式的不同,基于网络协议的套接字一般分为TCP套接字和UDP套接字.因为TCP套接字是面向连接的,因此又称为基于流(stream)的套接字.TCP是Transmissi ...

  5. TCP/IP网络编程之基于TCP的服务端/客户端(二)

    回声客户端问题 上一章TCP/IP网络编程之基于TCP的服务端/客户端(一)中,我们解释了回声客户端所存在的问题,那么单单是客户端的问题,服务端没有任何问题?是的,服务端没有问题,现在先让我们回顾下服 ...

  6. 动手学习TCP:服务端状态变迁

    上一篇文章介绍了TCP状态机,并且通过实验了解了TCP客户端正常的状态变迁过程. 那么,本篇文章就一起看看TCP服务端的正常状态变迁过程 服务端状态变迁 根据上一篇文章中的TCP状态变迁图,可以得到服 ...

  7. 通过Java WebService接口从服务端下载文件

    一. 前言 本文讲述如何通过webservice接口,从服务端下载文件.报告到客户端.适用于跨系统间的文件交互,传输文件不大的情况(控制在几百M以内).对于这种情况搭建一个FTP环境,增加了系统部署的 ...

  8. Python后端(一)——客户端/服务端

    网址组成(四部分) 协议      http, https(https 是加密的http) 主机      g.cn zhihu.com之类的网址 ,因此一般不用填写 路径      下面的「/」和「 ...

  9. win10操作系统下oracle11g客户端/服务端的下载安装配置卸载总结

    win10操作系统下oracle11g客户端/服务端的下载安装配置卸载总结 一:前提 注意:现在有两种安装的方式 1. oracle11g服务端(64位)+oracle客户端(32位)+plsql(3 ...

随机推荐

  1. oc-08-内存分析

    说有对象公用类的一个方法

  2. 第1章 游戏之乐——NIM(2)“拈”游戏分析

    NIM(2)“拈”游戏分析 1. 问题 有N块石头和两个玩家A和B,玩家A先将石头分成若干堆,然后按照BABA……的顺序不断轮流取石头,能将剩下的石头一次取光的玩家获胜.每次取石头时,每个玩家只能从若 ...

  3. System.Data.SQLite.EF6

    2015.1.21 到目前为止这个破玩意不支持code first 建数据库 建表 代替方案   SQL Server Compact -------------------------------- ...

  4. sqlserver 日期相关2

    1.常用日期方法(下面的GetDate() = '2006-11-08 13:37:56.233') (1)DATENAME ( datepart ,date ) 返回表示指定日期的指定日期部分的字符 ...

  5. focusky

    Focusky,是一款新型多媒体幻灯片制作软件,操作便捷性以及演示效果超越PPT,主要通过缩放.旋转.移动动作使演示变得生动有趣.传统PPT单线条时序,只是一张接一张切换播放,而Focusky打破常规 ...

  6. javascript获取鼠标位置

    首先不同浏览器中event位置属性的分析: 1. IE的event.x,event.y是以事件触发元素的父元素外界为参考点(不包括滚动距离) 2. Firefox的event.pageX,event. ...

  7. Wireshark抓包工具使用教程以及常用抓包规则

    转载:http://fangxin.blog.51cto.com/1125131/735178 Wireshark是一个非常好用的抓包工具,当我们遇到一些和网络相关的问题时,可以通过这个工具进行分析, ...

  8. css预处理的引入与问题

    css的预处理越来越流行.sass,less,stylus这几个都使用方便. 我想使用他的原因,暂时最主要是为了@import的功能.现在的问题:因为产品太多,是个页面,有10个css.而这10个cs ...

  9. WdatePicker时间控件联动选择

    $("#txtStartTime").bind("click focus", function () { var endtimeTf = $dp.$('txtE ...

  10. 配置hibernate例子

    一.hiberbate.cfg.xml <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hib ...