2009-07-03 35 views
8

我需要将文件传输到我的Web服务器进行处理,如果可能,我希望以通用方式进行处理。Java文件传输API

我需要能够至少从以下协议传输文件(有更多的最终跟随):

HTTP
FTP
SCP

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

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

如果没有工具包已经做到这一点,那么构建一个能够处理大多数文件传输的接口的最佳方法是什么?

我想过这样的事情:

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或协议并实例正确的文件处理程序的工厂。

+0

它是否必须是开源的或你愿意为解决方案付费? – amischiefr 2009-07-03 12:39:49

+0

我的项目本身就是开源的。所以无论我需要一个开放的解决方案还是自己推出。我已经开始整合Apache VFS。 – 2009-07-04 22:12:12

回答

6

Apache commons VFS说明了这个问题,虽然快速检查并未显示它会执行SCP或SMTP。 Commons NET确实SMTP,但我不知道你可以获得开箱即用的通用界面。对于SCP,这里有一些可能性。

底线似乎是检查VFS实现,看看它是否为你做了什么,也许你可以扩展它为不同的协议。如果它不合适,关于你的接口,你可能会希望所有的远程文件引用都是Strings而不是File对象,特别是一个表示指向远程位置的URI的字符串,并告诉你使用什么协议。

+0

可能有必要使用多个库,因为一个库可能不支持你想要的任何东西。 VFS确实支持SFTP,但不支持SMTP。 – Jesse 2009-07-03 13:07:39

2

我正在处理与您非常相似的问题,我无法找到任何开源解决方案,因此我正在尝试自己绘制解决方案。这是我想出来的。

我想你应该代表inputSources和outputSources不同的东西,比如

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) 

那么你应该有一个运动来描述输入应该去哪个输出。

class Movement{ 
     String inputId; 
     String outputId; 
} 

一个类来描述运动列表。

class MovementDescriptor{ 
     public addMovement(Movement a); 
     public Movement[] getAllMovements(); 
} 

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

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) 
      } 
    } 
} 

,将使用这将是这样操作的代码:

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。

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

0

请使用JCraft。打开“sftp”频道并尝试。