代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
socket接收输入流问题
作者:
xulong1516519
/ 发布于
2012/11/13
/
792
这是代码: 客户端: public class Client { static Socket client = null; static final int PORT = 1234; static final String IP = "127.0.0.1"; static final String PATH = "savefile.xml"; // static InputStream inputStream = null; static DataInputStream inputStream = null; static OutputStream outputStream = null; public static void main(String[] A) { init(); receiveData1(); } public static void init() { try { client = new Socket(IP, PORT); outputStream = new FileOutputStream(PATH); } catch (UnknownHostException e) { e.printStackTrace(); System.out.println("未知的服务器."); } catch (IOException e) { e.printStackTrace(); System.out.println("输入输出服务器出现异常."); } } public static void receiveData1() { try { // inputStream = client.getInputStream(); inputStream= new DataInputStream(new BufferedInputStream(client.getInputStream())); long startTime= System.currentTimeMillis(); String fileName= inputStream.readUTF(); long length= inputStream.readLong(); System.out.println("文件名以及长度:"+fileName+ " :"+ length); StringBuffer content= new StringBuffer(); byte[] temp= new byte[102400]; int numFlag= inputStream.read(temp); System.out.println("numFlag:"+numFlag); while(numFlag!= -1) { content.append(temp); numFlag= inputStream.read(temp); System.out.println("numFlag:"+numFlag); } System.out.println("接受的总时间是:"+(System.currentTimeMillis()- startTime)); SAVEINFILE(content); } catch (IOException e) { e.printStackTrace(); System.out.println("接收服务器返回数据时出现异常."); } } public static void receiveData2() { try { // inputStream = client.getInputStream(); byte[] temp= new byte[1000000]; StringBuffer content= new StringBuffer(); int numFlag= inputStream.read(temp); while(numFlag!= -1) { content.append(temp); numFlag= inputStream.read(temp); } System.out.println(content.toString()); SAVEINFILE(content); } catch (IOException e) { e.printStackTrace(); System.out.println("接收服务器返回数据时出现异常."); } } public static final void SAVEINFILE(final Object content) { System.out.println("content的类是:"+content.getClass()); if(content instanceof StringBuffer) { try { System.out.println(content); outputStream.write(content.toString().getBytes()); } catch (IOException e) { e.printStackTrace(); System.out.println("存储到文件中出现异常."); } }else{ if(content instanceof String) { try { outputStream.write(((String) content).getBytes()); } catch (IOException e) { e.printStackTrace(); System.out.println("存储到文件中出现异常."); } }else{ System.out.println("传入的参数没有定义."); } } CLOSE(); } public static final void CLOSE() { try { outputStream.flush(); inputStream.close(); outputStream.close(); } catch (IOException e) { e.printStackTrace(); System.out.println("关闭输入输出流的时候出现异常."); } } } 服务端: public class Server { static ServerSocket server= null; // static OutputStream outputStream= null; static DataOutputStream outputStream= null; static InputStream inputStream= null; static String fileName= ""; static long length= 0; static final int PORT= 1234; static Socket socket= null; static final String PATH= "E:/download/logs/test/SystemOut.log"; public static void main(String[] A) { init(); sendFile(); } public static void init() { try { server= new ServerSocket(PORT); } catch (IOException e) { e.printStackTrace(); System.out.println("服务器启动出现异常"); } System.out.println("服务器已启动."); } public static final void saveFile(final String path, Object content) { /*try { outputStream= new FileOutputStream(path); } catch (FileNotFoundException e) { e.printStackTrace(); System.out.println("读取文件时出现异常."); }*/ } public static void readFile(final String path) { try { File file= new File(path); fileName= file.getName(); length= file.length(); inputStream= new FileInputStream(file); } catch (FileNotFoundException e) { e.printStackTrace(); System.out.println("存储文件时出现异常."); } } public static void sendFile() { while(true) { try { socket = server.accept(); readFile(PATH); // outputStream= socket.getOutputStream(); outputStream= new DataOutputStream(new BufferedOutputStream(socket.getOutputStream())); } catch (IOException e) { e.printStackTrace(); System.out.println("等待客户端连接时出现错误."); } System.out.println("开始发送数据."); try { int i= 2; outputStream.writeUTF(fileName); outputStream.flush(); outputStream.writeLong(length); outputStream.flush(); byte[] temp= new byte[1024000]; int n= inputStream.read(temp); System.out.println("本次发送"+n+"个字节."); while(n!= -1) { System.out.println("本次发送了"+n+"个字节;"+"第"+i); outputStream.write(temp); n= inputStream.read(temp); i++; } CLOSE_CONNECTION(); } catch (IOException e) { e.printStackTrace(); System.out.println("发送数据时出现错误."); } } } public static void receiveFile() { try { inputStream= socket.getInputStream(); } catch (IOException e) { e.printStackTrace(); System.out.println("接收文件时出现异常."); } } public static final void CLOSE_CONNECTION() { try { outputStream.flush(); outputStream.close(); socket.close(); // server.close(); } catch (IOException e) { e.printStackTrace(); } } }
试试其它关键字
socket
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
xulong1516519
贡献的其它代码
(
4
)
.
复制域账号到剪贴板
.
文件分割+AES加密+Base64编码
.
socket接收输入流问题
.
dom4j操作xml
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3