JAVA-RMI实现大文件传输
2013-07-29 11:36
357 查看
在使用java-rmi的过程中,必然会遇到一个文件上传的问题,由于在rmi中无法传输文件流(比如rmi中的方法参数不能是FileInputStream之类的),那么我们只好选择一种折中的办法,就是先用FileInputStream将文件读到一个Byte数组中,然后把这个Byte数组作为参数传进RMI的方法中,然后在服务器端将Byte数组还原为outputStream,这样就能通过RMI来传输文件了 JAVA-RMI实现大文件传输,具体代码如下:
[代码]FileClient
package rmiupload; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.net.MalformedURLException; import java.rmi.Naming; import java.rmi.NotBoundException; import java.rmi.RemoteException; public class FileClient { public FileClient() { // TODO Auto-generated constructor stub } public static void main(String[] args) { try { FileDataService fileDataService = (FileDataService) Naming.lookup("rmi://localhost:9001/FileDataService"); fileDataService.upload("/Users/NeverDie/Documents/test.mp4", new FileClient().fileToByte("/Users/NeverDie/Music/test.mp4")); } catch (MalformedURLException | RemoteException | NotBoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //这个方法比较重要,通过这个方法把一个名为filename的文件转化为一个byte数组 private byte[] fileToByte(String filename){ byte[] b = null; try { File file = new File(filename); b = new byte[(int) file.length()]; BufferedInputStream is = new BufferedInputStream(new FileInputStream(file)); is.read(b); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return b; } }转载请注明来源稳砜诚信在线http://www.wind-fixasia.com
[代码]FileClient
package rmiupload; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.net.MalformedURLException; import java.rmi.Naming; import java.rmi.NotBoundException; import java.rmi.RemoteException; public class FileClient { public FileClient() { // TODO Auto-generated constructor stub } public static void main(String[] args) { try { FileDataService fileDataService = (FileDataService) Naming.lookup("rmi://localhost:9001/FileDataService"); fileDataService.upload("/Users/NeverDie/Documents/test.mp4", new FileClient().fileToByte("/Users/NeverDie/Music/test.mp4")); } catch (MalformedURLException | RemoteException | NotBoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //这个方法比较重要,通过这个方法把一个名为filename的文件转化为一个byte数组 private byte[] fileToByte(String filename){ byte[] b = null; try { File file = new File(filename); b = new byte[(int) file.length()]; BufferedInputStream is = new BufferedInputStream(new FileInputStream(file)); is.read(b); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return b; } }转载请注明来源稳砜诚信在线http://www.wind-fixasia.com
相关文章推荐
- [毕业设计-基于android的手机网盘的设计与实现] java中文件的socket传输问题
- JAVA应用XFire框架来实现WebServie的大文件传输功能之一(下载)
- 用Java Serialization实现任意文件网络传输
- 【代码笔记】Java深入学习——实现客户端发送文件到服务器的文件传输
- 用Java写个ftp传输类实现文件的上传和下载,用ikvmc转成dll
- 【JAVA】利用java-RMI进行大文件传输
- java实现SFTP传输文件例子
- Java基于Socket的文件传输实现方法
- JAVA应用XFire框架来实现WebServie的大文件传输功能之二(上传)
- java使用rmi传输大文件示例分享
- 使用java实现windows与linux之间的文件传输
- 用Java写个ftp传输类实现文件的上传和下载,用ikvmc转成dll
- UDP:Java实现简单大文件传输 分类: 网络通讯 2014-09-21 20:21 319人阅读 评论(0) 收藏
- java实现TCP与UDP混合传输文件总结
- UDP:Java实现简单大文件传输 分类: 网络通讯 2014-09-21 20:21 320人阅读 评论(0) 收藏
- UDP:Java实现简单大文件传输
- 用Java Serialization实现任意文件网络传输
- JAVA应用XFire框架来实现WebServie的大文件传输功能之一(下载)
- 超大文件上传到服务器,实现流式传输,不再出现java.lang.OutOfMemoryError: Java heap space
- 用Java Serialization实现任意文件网络传输