代码语言
.
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
】
利用java-RMI进行大文件传输
作者:
萧宇宙
/ 发布于
2013/5/28
/
550
简单模拟RMI传输大文件。
FileDataService import java.rmi.Remote; import java.rmi.RemoteException; public interface FileDataService extends Remote { public static final String FILE_SERVICE = "rmi://localhost:9001/FileDataService"; public String start(String filename) throws RemoteException; public void upload(String uploadId,byte[] part,int offset,int len) throws RemoteException; public void finish(String uploadId) throws RemoteException; } FileDataServiceImpl import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; import java.util.HashMap; import java.util.Map; import java.util.UUID; public class FileDataServiceImpl extends UnicastRemoteObject implements FileDataService { Map<String, OutputStream> os = new HashMap<String, OutputStream>(); public FileDataServiceImpl() throws RemoteException { super(); } public String start(String filename) throws RemoteException{ File file = new File(filename); try { if (!file.exists()) file.createNewFile(); OutputStream upOS = new BufferedOutputStream(new FileOutputStream(file)); String uploadId = UUID.randomUUID().toString(); os.put(uploadId, upOS); return uploadId; }catch (Exception e) { throw new RemoteException("初始化文件上传失败:",e); } } public void upload(String uploadId,byte[] part,int offset,int len) throws RemoteException { OutputStream upOS = os.get(uploadId); if(upOS == null){ throw new RemoteException("上传文件的id不存在:" + uploadId); } try { upOS.write(part,offset,len); } catch (Exception e) { throw new RemoteException("File Up[" + uploadId+"]Exception:",e); } } public void finish(String uploadId) throws RemoteException{ try { os.get(uploadId).close(); } catch (IOException e) { throw new RemoteException("结束文件上传失败:",e); } } } FileServer import java.net.MalformedURLException; import java.rmi.Naming; import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; public class FileServer { FileDataService fileDataService; public FileServer() { try { fileDataService = new FileDataServiceImpl(); LocateRegistry.createRegistry(9001); Naming.rebind(FileDataService.FILE_SERVICE, fileDataService); } catch (RemoteException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } } /** * @param args */ public static void main(String[] args) { new FileServer(); } } [Java]代码 import java.io.FileInputStream; import java.rmi.Naming; public class FileClient { public FileClient() { } public static void main(String[] args) { try { FileInputStream fis = new FileInputStream("d:/r1.log"); FileDataService fileDataService = (FileDataService) Naming.lookup(FileDataService.FILE_SERVICE); String uploadId = fileDataService.start("d:/r12.log"); byte[] buffer = new byte[1024]; int len = fis.read(buffer); while(len> -1){ fileDataService.upload(uploadId, buffer, 0, len); System.out.println("read part"); len = fis.read(buffer); } fileDataService.finish(uploadId); System.out.println("file upload finished"); } catch (Exception e) { e.printStackTrace(); } } }
试试其它关键字
大文件传输
同语言下
.
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转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
萧宇宙
贡献的其它代码
(
1
)
.
利用java-RMI进行大文件传输
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3