java rmi 文件传输_JAVA-RMI实现大文件传输

在使用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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值