基础 - 使用Java读取/写入远程文件 [英] Basics - reading/writing remote files using Java

查看:726
本文介绍了基础 - 使用Java读取/写入远程文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



首先,我编写了一个Java程序,可以读取,写入,读取和写入文件,从远程Windows机器上的共享文件夹,即在局域网上写入文件。在这里,像这样的东西在我的(本地)Windows计算机上工作:

  File inputFile = new File(
\\ \\\172.17.89.76\EBook PDF); / *忽略语法错误,loc是只是想法* /

现在当我考虑一台远程Ubuntu机器时,显然我不能这样做,因为机器不在局域网上(我不知道这是否可以完成,即使它是在局域网上!)。因此,我尝试了以下方法:使用Jsch,建立两台机器(本地 - 远程Linux,远程Linux - 远程Linux)之间的信任,

  • 在两台机器上运行套接字 - 一个发送者,一个接收者(两个Java)(完成)

  • 尝试实现I / O一样Windows(LAN)机器的代码段(未实现)

    在完成所有这些工作的同时,许多查询,阅读很多帖子等,我觉得我在基础知识上缺少一些东西:


    • 某种形式的信任构建两台机器之间)实用程序将需要实现IO。但最后,我想写一个类似于代码片段的代码,而不考虑机器,网络等。
    • Jsch解决方案和其他人建议(使用http,ftp等通过URL )终于在使用远程机器上运行的一些服务。换句话说,并不是说Java IO被用来访问远程文件系统 - 这对我来说并不吸引,因为我依靠的是服务而不是使用古老的I / O。
    • Samba,SSHFS也被弹出到现场,只是为了增添我的困惑。但我不认为它们是我的目标的解决方案!


      重申一遍,我想用一个代码Java I / O(plain或nio,都很好),只需读取,写入远程文件,而无需通过诸如ftp,http等协议的服务或套接字发送者 - 接收者模型。我的期望值是否有效?




      • 如果不是这样,为什么以及什么是最好的做法来读/写远程文件
        using Java?

      • 如果是的话,如何达到同样的效果!



      如果你想访问远程计算机上的文件系统,那么这个问题需要详细说明,以便准确地提出我的问题! 计算机必须使他的文件系统可用一项服务。这种服务通常是一个后台工作,处理传入的请求并返回响应,例如,用于认证,授权,读写。请求/响应模式的规范称为协议。众所周知的协议是Windows上的SMB(或SAMBA)或UNIX / LINUX上的NFS。要访问这样一个远程服务,您需要在操作系统级别安装远程文件系统,并在Windows上将其作为驱动器在本地使用,或者在UNIX上作为挂载点使用。



      然后你就可以从你的Java程序中像任何本地文件系统一样访问远程文件系统。

      当然,也可以编写你自己的文件服务提供者协议层)并在远程机器上运行。可以使用这种努力套接字(TCP / IP)的传输层。另一个好的传输层将是http协议,例如,一个宁静的服务或基于WebDav的东西。


      I started with requirement of reading and writing files in from/in a directory on a remote Ubuntu machine.

      First, I wrote a Java program that could read,write files from a shared folder on a remote Windows machine i.e on a LAN. Here, something like this works on my(local) Windows machine :

      File inputFile = new File(
                      "\\172.17.89.76\EBook PDF");/*ignore the syntax errors, the loc is just for the idea*/
      

      Now when I consider a remote Ubuntu machine, obviously I cannot do something like this as the machine is not on the LAN(I'm not sure if that can be done even if it is on the LAN!). Hence, I tried following approaches :

      1. Using Jsch, establishing the trust between two machines(local - remote Linux , remote Linux - remote Linux) and file writing using sftp.(done)
      2. Running sockets on the two machines - one sender, one receiver(both Java)(done)
      3. Attempting to achieve I/O alike the code snippet for Windows (LAN) machines(not achieved)

      While doing all these, I had many queries, read many posts etc. and I felt that I'm missing something on the fundamentals :

      • Some sort of trust-building(between two machines) utility will be required to achieve IO. But finally, I want to write a code like the snippet given, irrespective of the machines, network etc.
      • The Jsch solution and the others suggested(usage of http, ftp etc. over URL) finally are using some services that are running on the remote machine. In other words, it is NOT THAT Java IO is being used to access the remote file system - this doesn't appeal to me as I'm relying on services rather than using good-old I/O.
      • Samba, SSHFS too popped onto the scene, only to add to my confusion. But I don't see them as the solutions to my objective !

      To reiterate, I want to write a code using Java I/O(either plain or nio, both are fine) which simply can read, write remote files without using services over protocols like ftp, http etc. or socket sender-receiver model. Is my expectation valid?

      • If not, why and what is the best I can do to read/write remote files using Java?
      • If yes, how to achieve the same !

      P.S : Please comment in case I need to elaborate to pose my question accurately !

      解决方案

      If you want to access a filesystem on a remote computer, then this computer has to make his filesystem available with a service. Such a service is typically a background job, which handles incoming requests and returns a response, e.g. for authentication, authorization, reading and writing. The specification of the request/response pattern is called a protocol. Well known protocols are SMB (or SAMBA) on Windows or NFS on UNIX/LINUX. To access such a remote service you mount the remote filesystem on the level of the operating system and make it available locally as a drive on Windows or as mount point on UNIX.

      Then you can access the remote file system from your Java program like any local file system.

      Of course it is also possible to write your own file service provider (with your own protocol layer) and run it on the remote machine. As transport layer for such an endeavor sockets (TCP/IP) can be used. Another good transport layer would be the http protocol, e.g. with a restful service or something based on WebDav.

      这篇关于基础 - 使用Java读取/写入远程文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆