Category Archives: apache-commons-vfs

Creating a directory in linux server using apache.commons.vfs(JAVA)

I'm able to transfer files using apache.commons.vfs(JAVA).

However I need to make directory in the server before transferring files. Refer the code below:

public static void upload(String hostName, String username, String password, String localFilePath, String remoteFilePath) {

    File file = new File(localFilePath);
    if (!file.exists())
        throw new RuntimeException("Error. Local file not found");

    StandardFileSystemManager manager = new StandardFileSystemManager();

    try {
        manager.init();

        // Create local file object
        FileObject localFile = manager.resolveFile(file.getAbsolutePath());

        // Create remote file object
        FileObject remoteFile = manager.resolveFile(createConnectionString(hostName, username, password, remoteFilePath), createDefaultOptions());
        /*
         * use createDefaultOptions() in place of fsOptions for all default
         * options - Ashok.
         */
        // Copy local file to sftp server
        remoteFile.copyFrom(localFile, Selectors.SELECT_SELF);

        System.out.println("File upload success");
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        manager.close();
    }
}

org.apache.commons.vfs2.FileSystemException: Could not close the output stream

I am using VFS 2.0 for implementing SFTP for my application. I have a single Thread which actually picks up the file and do SFTP. 98% of the time it works. But I have observed there are few cases where SFTP is getting failed with below exception..

Caused by: org.apache.commons.vfs2.FileSystemException: Could not close the output stream for file "sftp://*******************/Test.csv".
    [null,null]at org.apache.commons.vfs2.provider.DefaultFileContent$FileContentOutputStream.close(DefaultFileContent.java:694)
    [null,null]at org.apache.commons.vfs2.FileUtil.copyContent(FileUtil.java:118)
    [null,null]at org.apache.commons.vfs2.provider.AbstractFileObject.copyFrom(AbstractFileObject.java:1053)
    [null,null]... 3 more
Caused by: java.io.IOException: inputstream is closed
    [null,null]at com.jcraft.jsch.ChannelSftp.fill(ChannelSftp.java:2884)
    [null,null]at com.jcraft.jsch.ChannelSftp.header(ChannelSftp.java:2908)
    [null,null]at com.jcraft.jsch.ChannelSftp.checkStatus(ChannelSftp.java:2446)
    [null,null]at com.jcraft.jsch.ChannelSftp._sendCLOSE(ChannelSftp.java:2465)
    [null,null]at com.jcraft.jsch.ChannelSftp.access$400(ChannelSftp.java:36)
    [null,null]at com.jcraft.jsch.ChannelSftp$1.close(ChannelSftp.java:854)
    [null,null]at java.io.FilterOutputStream.close(FilterOutputStream.java:143)
    [null,null]at org.apache.commons.vfs2.util.MonitorOutputStream.close(MonitorOutputStream.java:56)
    [null,null]at java.io.FilterOutputStream.close(FilterOutputStream.java:143)
    [null,null]at org.apache.commons.vfs2.util.MonitorOutputStream.close(MonitorOutputStream.java:56)
    [null,null]at org.apache.commons.vfs2.provider.DefaultFileContent$FileContentOutputStream.close(DefaultFileContent.java:690)
    [null,null]... 5 more

Please suggest what could be the possible reason for the above exception.

How do I use JCIFS with apache VFS to access an SMB URL – part 2?

I had the same problem: How do I use JCIFS with apache VFS to access an SMB URL?

... after including commons-vfs-sandbox-2.1-SNAPSHOT.jar in the classpath I now get this exception:

Exception in thread "main" org.apache.commons.vfs2.FileSystemException: Could not determine the type of file "smb://10.10.18.210/CIFS/123/asd".
    at org.apache.commons.vfs2.provider.AbstractFileObject.attach(AbstractFileObject.java:1522)
    at org.apache.commons.vfs2.provider.AbstractFileObject.getType(AbstractFileObject.java:489)
    at org.apache.commons.vfs2.provider.AbstractFileObject.exists(AbstractFileObject.java:477)
    at VFSTest.main(VFSTest.java:19)
Caused by: jcifs.smb.SmbAuthException: Logon failure: account currently disabled.
    at jcifs.smb.SmbTransport.checkStatus(SmbTransport.java:549)
    at jcifs.smb.SmbTransport.send(SmbTransport.java:667)
    at jcifs.smb.SmbSession.sessionSetup(SmbSession.java:390)
    at jcifs.smb.SmbSession.send(SmbSession.java:218)
    at jcifs.smb.SmbTree.treeConnect(SmbTree.java:176)
    at jcifs.smb.SmbFile.doConnect(SmbFile.java:911)
    at jcifs.smb.SmbFile.connect(SmbFile.java:954)
    at jcifs.smb.SmbFile.connect0(SmbFile.java:880)
    at jcifs.smb.SmbFile.queryPath(SmbFile.java:1335)
    at jcifs.smb.SmbFile.exists(SmbFile.java:1417)
    at jcifs.smb.SmbFile.isDirectory(SmbFile.java:1490)
    at org.apache.commons.vfs2.provider.smb.SmbFileObject.createSmbFile(SmbFileObject.java:118)
    at org.apache.commons.vfs2.provider.smb.SmbFileObject.doAttach(SmbFileObject.java:70)
    at org.apache.commons.vfs2.provider.AbstractFileObject.attach(AbstractFileObject.java:1505)
    ... 3 more

Please advice.