- 浏览: 272965 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
tan_1208815066:
传送pdf 的文件 不能正确的 传送
试试用Socket传文件 -
richardri:
结果是0怎么解决?支持mov、mp4、3gp吗?
JAVA獲取視頻文件的播放長度 -
zhujia130:
xiaoyaodandan 写道结果是0.。。。。。你给的路径 ...
JAVA獲取視頻文件的播放長度 -
xiaoyaodandan:
结果是0.。。。。。
JAVA獲取視頻文件的播放長度 -
flowerjacky:
明了
Java事务处理类
非常简单的一个传文件的功能;为多个客户端每天定时分配资料,Timer没有加进去,参考参考!
服务端:
服务端:
package cn.jatula.socket; import java.io.BufferedInputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.net.ServerSocket; import java.net.Socket; /** * 服务器端 * @author lake * */ public class ServerTest { int port = 8821; void start() { Socket s = null; try { ServerSocket ss = new ServerSocket(port); while (true) { // 选择进行传输的文件 String filePath = "C:\\jatula.rar"; File fi = new File(filePath); System.out.println("文件长度:" + (int) fi.length()); // public Socket accept() throws // IOException侦听并接受到此套接字的连接。此方法在进行连接之前一直阻塞。 s = ss.accept(); System.out.println("建立socket链接"); DataInputStream dis = new DataInputStream( new BufferedInputStream(s.getInputStream())); dis.readByte(); DataInputStream fis = new DataInputStream( new BufferedInputStream(new FileInputStream(filePath))); DataOutputStream ps = new DataOutputStream(s.getOutputStream()); //将文件名及长度传给客户端。这里要真正适用所有平台,例如中文名的处理,还需要加工。 ps.writeUTF(fi.getName()); ps.flush(); ps.writeLong((long) fi.length()); ps.flush(); int bufferSize = 8192; byte[] buf = new byte[bufferSize]; while (true) { int read = 0; if (fis != null) { read = fis.read(buf); } if (read == -1) { break; } ps.write(buf, 0, read); } ps.flush(); // 注意关闭socket链接哦,不然客户端会等待server的数据过来, // 直到socket超时,导致数据不完整。 fis.close(); s.close(); System.out.println("文件传输完成"); } } catch (Exception e) { e.printStackTrace(); } } public static void main(String arg[]) { new ServerTest().start(); } }
客户端:
package cn.jatula.socket; import java.io.BufferedOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileOutputStream; /** * 客户端 * @author lake * */ public class ClientTest { private ClientSocket cs = null; private String ip = "localhost";// 设置成服务器IP private int port = 8821; private String sendMessage = "Windwos"; public ClientTest() { try { if (createConnection()) { sendMessage(); getMessage(); } } catch (Exception ex) { ex.printStackTrace(); } } private boolean createConnection() { cs = new ClientSocket(ip, port); try { cs.CreateConnection(); System.out.print("连接服务器成功!" + "\n"); return true; } catch (Exception e) { System.out.print("连接服务器失败!" + "\n"); return false; } } private void sendMessage() { if (cs == null) return; try { cs.sendMessage(sendMessage); } catch (Exception e) { System.out.print("发送消息失败!" + "\n"); } } private void getMessage() { if (cs == null) return; DataInputStream inputStream = null; try { inputStream = cs.getMessageStream(); } catch (Exception e) { System.out.print("接收消息缓存错误\n"); return; } try { // 本地保存路径,文件名会自动从服务器端继承而来。 String savePath = "E:\\"; int bufferSize = 8192; byte[] buf = new byte[bufferSize]; int passedlen = 0; long len = 0; savePath += inputStream.readUTF(); DataOutputStream fileOut = new DataOutputStream( new BufferedOutputStream(new BufferedOutputStream( new FileOutputStream(savePath)))); len = inputStream.readLong(); System.out.println("文件的长度为:" + len + "\n"); System.out.println("开始接收文件!" + "\n"); while (true) { int read = 0; if (inputStream != null) { read = inputStream.read(buf); } passedlen += read; if (read == -1) { break; } // 下面进度条本为图形界面的prograssBar做的,这里如果是打文件,可能会重复打印出一些相同的百分比 System.out.println("文件接收了" + (passedlen * 100 / len) + "%\n"); fileOut.write(buf, 0, read); } System.out.println("接收完成,文件存为" + savePath + "\n"); fileOut.close(); } catch (Exception e) { System.out.println("接收消息错误" + "\n"); return; } } public static void main(String arg[]) { new ClientTest(); } }
辅助类
package cn.jatula.socket; import java.net.*; import java.io.*; /** * socket的Util辅助类 * @author lake * */ public class ClientSocket { private String ip; private int port; private Socket socket = null; DataOutputStream out = null; DataInputStream getMessageStream = null; public ClientSocket(String ip, int port) { this.ip = ip; this.port = port; } /** */ /** * 创建socket连接 * * @throws Exception * exception */ public void CreateConnection() throws Exception { try { socket = new Socket(ip, port); } catch (Exception e) { e.printStackTrace(); if (socket != null) socket.close(); throw e; } finally { } } public void sendMessage(String sendMessage) throws Exception { try { out = new DataOutputStream(socket.getOutputStream()); if (sendMessage.equals("Windows")) { out.writeByte(0x1); out.flush(); return; } if (sendMessage.equals("Unix")) { out.writeByte(0x2); out.flush(); return; } if (sendMessage.equals("Linux")) { out.writeByte(0x3); out.flush(); } else { out.writeUTF(sendMessage); out.flush(); } } catch (Exception e) { e.printStackTrace(); if (out != null) out.close(); throw e; } finally { } } public DataInputStream getMessageStream() throws Exception { try { getMessageStream = new DataInputStream(new BufferedInputStream( socket.getInputStream())); return getMessageStream; } catch (Exception e) { e.printStackTrace(); if (getMessageStream != null) getMessageStream.close(); throw e; } finally { } } public void shutDownConnection() { try { if (out != null) out.close(); if (getMessageStream != null) getMessageStream.close(); if (socket != null) socket.close(); } catch (Exception e) { } } }
评论
5 楼
tan_1208815066
2013-03-18
传送pdf 的文件 不能正确的 传送
4 楼
bupt_烦人
2010-03-16
int passedlen ;建议改为long,当长度超过20M的时候会出现越界
3 楼
Jatula
2008-03-18
快得看不见手,在本地测试的,所以没办法给你答案;
没做过压力测试,哈哈;
没做过压力测试,哈哈;
2 楼
oklzh
2008-03-17
无聊,研究研究
1 楼
javaeyename
2008-03-17
速度怎么样?有没有统计?
发表评论
-
junit实现测试类(在Spring2.5中)
2009-01-17 15:14 2370第一步: 创建JAVA项目,也可以是Web项目,加入Sprin ... -
HttpClient Examples:Custom protocol interceptors
2009-01-15 14:06 1475官方主頁:http://hc.apache.org/ Co ... -
HttpClient Examples:Custom SSL context
2009-01-15 13:58 2002官方主頁:http://hc.apache.org/ Co ... -
HttpClient Examples:Threaded request execution
2009-01-15 13:54 1442官方主頁:http://hc.apache.org/ Co ... -
HttpClient Examples:Form based logon
2009-01-15 13:49 1968官方主頁:http://hc.apache.org/ Co ... -
HttpClient Examples:Custom execution context
2009-01-15 13:46 1518官方主頁:http://hc.apache.org/ Co ... -
HttpClient Examples:Chunk encoded POST
2009-01-15 12:36 2146官方主頁:http://hc.apache.org/ Co ... -
HttpClient Examples:Proxy authentication
2009-01-15 12:26 1342官方主頁:http://hc.apache.org/ Co ... -
HttpClient Examples:Request via a proxy
2009-01-15 12:19 2173官方主頁:http://hc.apache.org/ Co ... -
HttpClient Examples:Abort method
2009-01-15 12:06 1564官方主頁:http://hc.apache.org/ Co ... -
HttpClient Examples:Manual connection release
2009-01-15 11:54 1150官方主頁:http://hc.apache.org/ Co ... -
HttpClient Examples:Response handling
2009-01-15 11:44 2273官方主頁:http://hc.apache.org/ Co ... -
使用commons-codec包加密字符串(MD5,SHA1,BASE64)
2009-01-13 13:56 8179commons-codec包可以从apache下载,最新版是1 ... -
Java对称加密编码:BESA64
2009-01-13 10:58 2450package cn.lake.util; import s ... -
Java对称加密编码:IDEA
2009-01-13 10:55 2083package cn.lake.util; /** * J ... -
用java获得你电脑中的cpu数量
2009-01-13 10:41 2326System.out.println("你的电脑cp ... -
JAVA的国际化问题讨论
2008-12-18 15:05 1037一切有一个问题相当苦脑:JAVA的国际化问题; 如 ... -
客户请求的浏览器类型
2008-12-17 00:17 880<td width="50%"> ... -
同时启动多个resion
2008-12-09 15:55 989在本地同时启动多个Resin,要修改2个文件,总共是3个处 一 ... -
JSP基本语法加实例
2008-12-09 13:38 1579应作者要求:【转】http://www.cnblogs.com ...
相关推荐
这个程序源码是本人上次发布的BCB网络文件传输客户端源码的第三版,这个版本在第二个版本的基础上,增加了文件断点下载功能,以供大家学习交流。最近老婆孩子都从老家回来了,而且我还想再试试P2P的C++视频聊天程序...
wx.connectSocket({ url: 'ws://localhost:9090/weappservice/websocket?name=xiaoqiang', //这里写了参数,但是参数没有发送出去,大家可以试试,已经邮件反馈微信团队了,等待回复。所以把参数拼接在url...
开发socket上传数据的时候,udp包有大小限制,所以需要按字节分割读取的文件。 网上找到的有没有合适的,就自己弄了一个。有需要可以试试。
-n:一般而言,mount挂上后会在/etc/mtab中写入一笔资料,在系统中没有可写入文件系统的情况下,可以用这个选项取消这个动作。 4.应用技巧 在Linux 和Unix系统上,所有文件都是作为一个大型树(以/为根)的一部分...
3.FlexPaper不能正常浏览文件的时候,可以试试在官网添加信任。网址http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04a.html#119065一定要添加信任 4.readFile.jsp中动态...
将mongodb Clone存储库安装到$ GOPATH / src / github.com / octohedron / goddit安装依赖项$ go获取构建二进制文件$ go bu Golang聊天服务器:ping_pong:================== ==========试试吧! 将mongodb Clone存储...
只包含几个需要破解的文件.解压覆盖到安装文件夹即可. 安装程序不包含在里面.请自行在官方网站下载. Ch是一个跨平台的C/C++脚本解释器,它支持ISO的C语言标准(C90/C99)和C++类库,附带了8000多个函数库并支持众多的...
Bin:服务端目录下面有三个文件 HPSocket4C.dll、SkinH.dll、sqlite3.dll、 NetDB.dll Bin:客户端目录下面有两个文件 HPSocket4C.dll、SkinH.dll、 NetDB.dll She:皮肤目录 说明: 1.模块调用NetDB支持库,必须要...
我想试试,如果没有框架,我将会写代码吗?学习下如何利用最基础的一些东西来开发一个网站。 边学习锈同时学习网站开发相关的一些知识,如简单的实现http协议,socket编程等等 锈是一门很难的语言,如果只是在别人...
A:扫描过程中系统蓝屏是有可能的,AtGuard、天网等防火墙的驱动程序在处理特殊包的时候有可能出错导致系统崩溃,另外很多防火墙驱动与WinPCap驱动本身也存在冲突,建议先禁止或卸载防火墙程序再试试。 Q:操作...
多线程扫描是这个扫描器的一大特色,所谓多线程就是说同时在本地系统开辟多个socket连接,在同一时间内扫描多个服务器,这样做的好处是提高了扫描速度,节省时间,根据系统的资源配置高低,线程数字也可以自行设定...
使用说明.txt -- X-Scan使用说明 /dat/language.ini -- 多语言配置文件,可通过设置“LANGUAGE\SELECTED”项进行语言切换 /dat/language.* -- 多语言数据文件 /dat/config.ini -- 用户配置文件,用于...
A:扫描过程中系统蓝屏是有可能的,AtGuard、天网等防火墙的驱动程序在处理特殊包的时候有可能出错导致系统崩溃,另外很多防火墙驱动与WinPCap驱动本身也存在冲突,建议先禁止或卸载防火墙程序再试试。 Q:操作...
个文件不妨试试另外的几个命令 /usr/local/mysql/bin/mysql -u root -p /usr/local/mysql/bin/mysqld --user=mysql& /usr/local/mysql/bin/mysqld --user=root& /usr/local/mysql/bin/mysqld_safe --user=root& /usr...
升级PORT插件,在无法创建Raw Socket时改为使用标准TCP连接方式检测开放端口。 X-Scan v1.1 -- 发布日期:11/25/2001,将所有检测功能移入插件,使主程序完全成为“容 器”;提供多语言支持;更新图形接口程序;...
分享给大家供大家参考。具体如下: ... 遗憾的是现在对中文的支持很差,英文聊天倒是没什么问题了。...废话不多,直接放代码了,就一个py文件而已,而且注释是乱七八糟的,编码风格也不好(好神似我在用类C语言的习惯)。
正常情况下MDK是不能同时支持51和AMM的,除非装两个,但是这样太麻烦了,这个修改方法很简单,已经用了很久了,不知道的可以试试。 首先先装ARM版,破解一下,然后把安装目录下的 TOOLS.INI 文件里面添加红字内容...