首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java文件传输API

Java文件传输API
EN

Stack Overflow用户
提问于 2009-07-03 12:31:28
回答 4查看 9K关注 0票数 9

我需要将文件传输到我的web服务器进行处理,如果可能的话,我想以一种通用的方式来完成。

我需要至少能够传输来自以下协议的文件(最终还会有更多的协议):

HTTP

FTP

SCP

我真的希望能够发送文件到SMTP也

所以我的问题是,有没有一个工具包已经可以做到这一点?如果是这样,它必须是开源的,因为这是开源项目的一部分。

如果还没有这样的工具包,那么构建一个处理大多数文件传输的接口的最佳方式是什么呢?

我想过这样的事情:

代码语言:javascript
复制
public interface FileTransfer {
    public void connect(URL url, String userid, String password);
    public void disconnect();
    public void getFile(String sourceFile, File destFile);
    public void putFile(File sourceFile, File destFile);
}

然后是一个工厂,它获取源URL或协议并实例化正确的文件处理程序。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2009-07-03 12:53:52

Apache commons VFS解决了这个问题,尽管快速检查没有显示它可以执行SCP或SMTP。Commons NET确实做了SMTP,但我不知道你能不能开箱即用。对于SCP,这里有一些可能性。

底线似乎是检查VFS实现,看看它是否能为您做一些事情,也许您可以将其扩展到不同的协议。如果这不合适,对于您的接口,您可能希望所有远程文件引用都是string,而不是file对象,具体地说,是一个表示指向远程位置的URI的字符串,并告诉您要使用什么协议。

票数 6
EN

Stack Overflow用户

发布于 2009-07-22 14:59:27

我正在处理一个和你的问题非常相似的问题,我找不到任何开源的解决方案,所以我试图自己勾勒出一个解决方案。这就是我想出来的。

我认为你应该把inputSources和outputSources表示成不同的东西,比如

代码语言:javascript
复制
public interface Input{
      abstract InputStream getFileInputStream();
      abstract String getStreamId();
}
//You can have differen implementation of this interface (1 for ftp, 1 for local files, 1 for Blob on db etc)  

public interface Output{
      abstract OutputStream getOutputStream();
      abstract String getStreamId();
}
//You can have differen implementation of this interface (1 for ftp, 1 for local files, 1 for mailing the file etc)  

然后你应该有一个移动来描述哪个输入应该转到哪个输出。

代码语言:javascript
复制
 class Movement{
        String inputId;
        String outputId;
 }

一个用于描述要进行的移动列表的类。

代码语言:javascript
复制
class MovementDescriptor{
       public addMovement(Movement a);
       public Movement[] getAllMovements();
}  

然后是一个执行工作本身的类。

代码语言:javascript
复制
class FileMover{

      HashMap<String,Input> inputRegistry;
      HashMap<String,Output> outputRegistry;

      addInputToRegistry(Input a ){
          inputRegistry.put(a.getId(),a);
      }
     addOutputToRegistry(Output a){
          outputRegistry.put(a.getId(),a);
      }

     transferFiles(MovementDescriptor movementDescriptor){

         Movement[] movements =movementDescriptor.getAllMovements();
         foreach (Movement movement: movements){
              //get the input Id
              //find it in the registry and retrieve the associated InputStream
              //get the output Id
              //find it in the registry and retrieve the associated OutputStream
              //copy the stream from the input to the output (you may want to use a temporary file in between)
          }
     }
}

使用此命令的代码的操作方式如下:

代码语言:javascript
复制
FileMover fm=new FileMover();

//Register your sources and your destinations
fm.addInputToRegistry(input);
fm.addOutputToRegistry(output)

// each time you have to make a movement create a MovementDescriptor and call
fm.transferFiles(movementDescriptor)

如果你想通过邮件交流我们对这一主题的看法,只需给我发一封电子邮件(我的昵称)@gmail.com。

注意:代码只是一个草图:-)

票数 2
EN

Stack Overflow用户

发布于 2009-07-22 15:06:08

我认为JSch实现了SCP,所以这就涵盖了这一点。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1079151

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档