为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用apache common vfs上传字节数组

如何使用apache common vfs上传字节数组

白衣非少年 2023-08-16 16:09:40
我陷入了使用 apache common vfs 上传字节数组的困境,下面是使用文件路径上传文件的示例,我用谷歌搜索但没有得到使用字节数组上传文件的解决方案    public static boolean upload(String localFilePath, String remoteFilePath) {    boolean result = false;    File file = new File(localFilePath);    if (!file.exists())        throw new RuntimeException("Error. Local file not found");    try (StandardFileSystemManager manager = new StandardFileSystemManager();            // Create local file object            FileObject localFile = manager.resolveFile(file.getAbsolutePath());            // Create remote file object            FileObject remoteFile = manager.resolveFile(                    createConnectionString(hostName, username, password, remoteFilePath),                    createDefaultOptions());) {        manager.init();        // Copy local file to sftp server        remoteFile .copyFrom(localFile, Selectors.SELECT_SELF);        result = true;    } catch (Exception e) {        throw new RuntimeException(e);    }    return result;}并且工作正常。下面是使用FTPSClient上传字节数组的代码 private boolean uploadFtp(FTPSClient ftpsClient, byte[] fileData, String fileName) { boolean completed = false; byte[] bytesIn = new byte[4096]; try (InputStream inputStream = new ByteArrayInputStream(fileData); OutputStream outputStream =  ftpsClient.storeFileStream(fileName);) {  int read = 0;  while ((read = inputStream.read(bytesIn)) != -1) {   outputStream.write(bytesIn, 0, read);  }  completed = ftpsClient.completePendingCommand();  if (completed) {   logger.info("File uploaded successfully societyId");  } } catch (IOException e) { logger.error("Error while uploading file to ftp server", e); } return completed;}
查看完整描述

2 回答

?
杨__羊羊

TA贡献1943条经验 获得超7个赞

这是一个老问题,但我遇到了同样的问题,并且我能够在不假设本地文件的情况下解决它,例如使用 ByteArrayInputStream,所以这对于与 pise 遇到相同问题的人可能很有用。基本上,我们可以将输入流直接复制到远程文件的输出流中。


代码是这样的:


InputStream is = ... // you only need an input stream, no local file, e.g. ByteArrayInputStream

    

DefaultFileSystemManager fsmanager = (DefaultFileSystemManager) VFS.getManager();

        

FileSystemOptions opts = new FileSystemOptions();

FtpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);

StaticUserAuthenticator auth = new StaticUserAuthenticator(host, username, password);

         

DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);

        

String ftpurl = "ftp://" + host + ":" + port + "/" + folder + "/" + filename;

FileObject remoteFile = fsmanager.resolveFile(ftpurl, opts);

        

try (OutputStream ostream = remoteFile.getContent().getOutputStream()) {

    // either copy input stream manually or with IOUtils.copy()

    IOUtils.copy(is, ostream);

}

        

boolean success = remoteFile.exists();

long size = remoteFile.getContent().getSize();

System.out.println(success ? "Successful, copied " + size + " bytes" : "Failed");


查看完整回答
反对 回复 2023-08-16
?
当年话下

TA贡献1890条经验 获得超9个赞

/**

 * Write the byte array to the given file.

 *

 * @param file The file to write to

 * @param data The data array

 * @throws IOException

 */


public static void writeData(FileObject file, byte[] data)

        throws IOException {

    OutputStream out = null;

    try {

        FileContent content = file.getContent();

        out = content.getOutputStream();

        out.write(data);

    } finally {

        close(out);

    }

}


查看完整回答
反对 回复 2023-08-16
  • 2 回答
  • 0 关注
  • 78 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信