Java 小程序上传文件和文件夹,同时保持目录结构完整

发布于 2024-11-17 06:50:50 字数 1633 浏览 2 评论 0原文

以下代码用于将文件和文件夹上传到服务器(SFTP)。我试图保持文件夹结构完整,即 C:\temp\test\file.txt 在服务器上变为 /home/www/javauploads/temp/test/file.txt 。但是当我运行它时,文件全部上传,但文件夹和文件只是转储到服务器上,根本没有任何结构,并且文件名看起来像这样(C3NXXV~9)是否有一种方法可以上传文件和文件夹,同时保留结构和文件名完好无损,谢谢。 `

    private static void processDir(File dir) throws JSchException, SftpException {
    String SFTPHOST = "*****.com";
    int    SFTPPORT = ***;
    String SFTPUSER = "****";
    String SFTPPASS = "*****";
    String SFTPWORKINGDIR = "/home/www/javauploads/";`Session     session     = null;
    Channel     channel     = null;
    ChannelSftp channelSftp = null;
    JSch jsch = new JSch();
    session = jsch.getSession(SFTPUSER,SFTPHOST,SFTPPORT);
    session.setPassword(SFTPPASS);
    java.util.Properties config = new java.util.Properties();
    config.put("StrictHostKeyChecking", "no");
    session.setConfig(config);
    session.connect();

    channel = session.openChannel("sftp");
    channel.connect();
    channelSftp = (ChannelSftp)channel;
    channelSftp.cd(SFTPWORKINGDIR);


    if (dir.isDirectory()){
        channelSftp.mkdir(dir.toString());
        System.out.println("[Directory] : " + dir);
    }else{
        channelSftp.put(dir.toString());
        System.out.println("[File] : " + dir);
    }
    channelSftp.exit();
    session.disconnect();
}

private static void traverse(File dir) throws JSchException, SftpException {

    processDir(dir);

    if (dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            traverse(new File(dir, children[i]));
        }
    }

}`

the following code is for uploading files and folders to a server(SFTP). i m trying to keep the folder structure intact i.e. C:\temp\test\file.txt becomes /home/www/javauploads/temp/test/file.txt on the server. but when i run it the files all upload but the folders and files are just dumped on the server without any structure at all and the file names appear like this (C3NXXV~9) is there a way to upload the files and folders while keeping the structure and file names intact, thanks. `

    private static void processDir(File dir) throws JSchException, SftpException {
    String SFTPHOST = "*****.com";
    int    SFTPPORT = ***;
    String SFTPUSER = "****";
    String SFTPPASS = "*****";
    String SFTPWORKINGDIR = "/home/www/javauploads/";`Session     session     = null;
    Channel     channel     = null;
    ChannelSftp channelSftp = null;
    JSch jsch = new JSch();
    session = jsch.getSession(SFTPUSER,SFTPHOST,SFTPPORT);
    session.setPassword(SFTPPASS);
    java.util.Properties config = new java.util.Properties();
    config.put("StrictHostKeyChecking", "no");
    session.setConfig(config);
    session.connect();

    channel = session.openChannel("sftp");
    channel.connect();
    channelSftp = (ChannelSftp)channel;
    channelSftp.cd(SFTPWORKINGDIR);


    if (dir.isDirectory()){
        channelSftp.mkdir(dir.toString());
        System.out.println("[Directory] : " + dir);
    }else{
        channelSftp.put(dir.toString());
        System.out.println("[File] : " + dir);
    }
    channelSftp.exit();
    session.disconnect();
}

private static void traverse(File dir) throws JSchException, SftpException {

    processDir(dir);

    if (dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            traverse(new File(dir, children[i]));
        }
    }

}`

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深爱不及久伴 2024-11-24 06:50:50

首先(与您的问题无关),您不应该为每个文件或目录启动新的会话和通道 - 打开会话和通道一次,然后将其重新用于所有文件上传。这会快得多,因为每个会话打开都需要一些往返和一些复杂的加密计算。打开通道需要另一个往返。

然后,看看如何 ChannelSftp.put(String) 有效。它将远程文件名作为参数,并返回一个 OutputStream,然后您可以将数据写入其中。您没有使用 OutputStream,这意味着实际上不会发生上传。

我想你想要 ChannelSftp.put(String, String) - 这将采用本地和远程文件名,并上传本地文件的内容,创建远程文件(或者覆盖它,如果它已经存在)。

另外,可能存在 dir.toString() 返回本地命名约定中的文件名(即在 Windows 系统上用 \ 分隔),而您需要用 /。我认为您可以使用简单的 .replace('\\', '/')

但这并不能真正解释您的观察结果 - 您在哪个系统上使用哪个 SSH 服务器?

First (unrelated to your problem), you should not start a new session and channel for each file or directory - open the session and channel once, and reuse it for all the file uploads. This will be much faster, since each session opening takes some roundtrips and some complicated cryptographic calculations. Opening a channel takes another roundtrip.

Then, take a look on how ChannelSftp.put(String) works. It takes the remote file name as argument, and returns an OutputStream, into which you then can write your data. You are not using the OutputStream, which means that no upload really will occur.

I suppose you want ChannelSftp.put(String, String) - this will take a local and remote file name, and upload the contents of the local file, creating a remote file (or overwriting it, if it already exists).

Also, there may be a problem that dir.toString() returns a file name in the local naming convention (i.e. separated by \ on Windows systems), while you need the names separated by /. You could use a simple .replace('\\', '/'), I think.

This does not really explain your observation, though - which SSH server are you using, on which system?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文