看看这段程序/////我调了两天两夜,就是得不到想要的结果,请问哪里出问题了???????????????

jolianasd 2008-03-23 05:26:37
package exer;
import java.io.*;
import java.net.*;
public class SerSocket {
public static void main(String[] args) {
ServerSocket ss = null;
Socket socket = null;
BufferedReader br = null;
InputStreamReader ndis = null;
BufferedWriter nbw = null;
try {
ss = new ServerSocket(8888);
socket = ss.accept();
br = new BufferedReader(new FileReader("e:\\杭州市地图.gif"));
ndis = new InputStreamReader(socket.getInputStream());
nbw = new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream()));
int i = 0;
i=ndis.read();
String s = null;
System.out.println(i);
while ((s = br.readLine()) != null) {
nbw.write(s);
nbw.flush();
System.out.println(i++);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
ss.close();
socket.close();
br.close();
ndis.close();
nbw.close();
} catch (IOException e) {
e.printStackTrace();
}

}

}
////////////////////////////////////////////////////////////////////////////////////////////
package exer;
import java.io.*;
import java.net.*;
public class Clisocket {
public static void main(String[] args) {
Socket socket=null;
BufferedWriter bw=null;
OutputStreamWriter nosw=null;
BufferedReader nbr=null;
try {
socket = new Socket("localhost", 8888);
bw = new BufferedWriter(new FileWriter("java1.jpg"));
nosw = new OutputStreamWriter(socket
.getOutputStream());
nbr = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
nosw.write(12);
String s=null;
int i=10000;
s=nbr.readLine();
while(s!=null){
bw.write(s);
bw.newLine();
s=nbr.readLine();
System.out.println(i++);
}
bw.flush();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
socket.close();
bw.close();
nosw.close();
nbr.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}
...全文
113 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
chch87 2008-03-23
  • 打赏
  • 举报
回复
给段代码你参考下吧

import java.io.*;
import java.net.*;
public class SerSocket {
public static void main(String[] args) {
File sourceFile=new File("Source.gif");
int bufferLength = 1024;
long fileLength = sourceFile.length();
byte[] buffer = new byte[bufferLength];
ServerSocket ss = null;
Socket socket = null;
OutputStream sOutputStream = null;
FileInputStream fInputStream = null;
try {
ss = new ServerSocket(8888);
System.out.println("启动");
socket = ss.accept();
System.out.println("已连接客户端");
sOutputStream = socket.getOutputStream();
fInputStream = new FileInputStream(sourceFile);
System.out.println("已传送:");
int len = 0;
long fileL = 0;
do{
len = fInputStream.read(buffer);//文件读到buffer
if(len != -1){
fileL += len;
sOutputStream.write(buffer,0,len);//buffer写入socket流
System.out.print("\b\b\b"+fileL*100/fileLength+"%");
}
}while(len != -1);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
ss.close();
socket.close();
sOutputStream.close();
fInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}

}

}



import java.io.*;
import java.net.*;
public class Clisocket {
public static void main(String[] args) {
Socket socket = null;
InputStream sInputStream = null;
FileOutputStream fOutputStream = null;
int bufferLength = 2048;
byte[] buffer = new byte[bufferLength];
File aimFile = new File("Aim.gif");
try {
socket = new Socket("localhost", 8888);
System.out.println("已连接服务端");
sInputStream = socket.getInputStream();
fOutputStream = new FileOutputStream(aimFile);
System.out.println("正在接受...");
int len = 0;
do{
len = sInputStream.read(buffer);//socket流读入buffer
if(len != -1)
fOutputStream.write(buffer,0,len);//buffer写入文件
}while(len != -1);
System.out.println("完毕");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
socket.close();
sInputStream.close();
fOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}


看你原来的代码,我猜你是想通过TCP通讯的例子举一反三吧,就是流相关的基础还需加强,看看一般的教材后再多思考就可以了.
kokobox 2008-03-23
  • 打赏
  • 举报
回复
用Stream+buffer的形式对图片和音频文件的输入输出。
igooogle 2008-03-23
  • 打赏
  • 举报
回复
读取图片需要使用二进制流读取。
AshEmissary 2008-03-23
  • 打赏
  • 举报
回复
br = new BufferedReader(new FileReader("e:\\杭州市地图.gif"));
你用字符流读图片?
资源说明】 基于深度学习的光伏发电功率预测系统源码(含前端+后端)+项目说明.zip 本项目主体围绕着“基于深度学习算法的短期光伏发电功率预测模型”而构建。旨在设计一款面向电力公司和光伏发电业主的系统,其具有辅助决策、风险预测的能力,强大的数据提取、统计与分析能力和清晰的数据相关性、影响性的可视化展示功能。 项目背景 随着光伏发电网规模的扩大和应用的普及,对于光伏发电的并网需求持续增大,但是光伏发电所具有的波动性和间歇性的特性势必会对并网系统的稳定性产生一定的影响。诸如太阳辐射强度、空气湿度、气压、环境温度等因素是导致光伏发电的波动性和间歇性产生的首要因素,若对于光伏发电缺少相应的技术手段预测功率和调整模式,势必在并网时会不利于电力系统的调整,并且为电网调度人员产生困扰。 在此背景之下,设计合理且有效的光伏发电功率预测模型一方面可以提升并网后电力系统运行的安全性和稳定性;另一方面也有利于光伏电站业主或调度人员的协调调度,进一步提升电能资源利用率。 项目目的 通过设计合理的“基于深度学习算法的短期光伏发电功率预测模型”以及对该模型的进一步优化,在大规模分布式光伏发电的电力管理层面,为管理人员提供短期内环境因素的具体数据以及光伏发电功率的预测结果,帮助管理人员进行协调调度,从而提升电能利用率;在个人中小规模分布式光伏电站业主层面,提供短期内光伏发电功率的预测结果并给调整策略建议,保障中小型分布式光伏电站并网后电网运行的稳定性和供电可靠性,同时对于一种更加互利且合理的个人光伏发电并网后差额电量的补偿和外送机制进行展望。 ## 技术选型(Technics) 项目架构 该项目采用B/S架构,后端调用算法模型,展示在前端。 算法技术 主要语言:Python - Keras - Sci-kit Learn 后端技术 主要语言:Java - Spring

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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