【问题标题】:java read file for ftp upload from jar filejava从jar文件中读取文件以进行ftp上传
【发布时间】:2012-11-01 06:05:38
【问题描述】:

我正在尝试使用 apache 公共库通过 ftp 上传文件。我想将这些文件保留在 jar 中,因为这些文件将保持不变,并且需要多次上传到不同的主机上。

我正在尝试以下方法:

    public void doCGIRecovery() throws IOException{
    System.out.println(getClass().getResource("/cgi/example").getPath());
    FileInputStream file = new FileInputStream(getClass().getResource("/cgi/example").getPath());
    f.storeFile("./", file);
}

使用 eclipse 这可以工作,系统输出正在给我:

/Users/user/Documents/workspace/example-Project/bin/com/example/java/example/business/cgi/example

但是在编译成 jar 之后,它会返回以下内容:

file:/Users/user/Desktop/example.jar!/com/example/java/example/business/cgi/example

我得到(没有这样的文件或目录)。

那是怎么来的呢? “!”在哪里? example.jar 之后是从哪里来的?

最好的问候

【问题讨论】:

标签: java ftp fileinputstream


【解决方案1】:

不用担心弄乱路径,使用 getClass().getResourceAsStream() 代替。

【讨论】:

  • 我也试过了,但问题是,getClass().getResourceAsStream() 返回一个输入流,我需要一个FileInputStream。而且我无法将 InputStream 转换为 FileInputStream。
  • 那么您可能会遇到问题,因为打包在 .jar 中的项目不再是单独的文件。标准的 File API 不能直接访问它们。
猜你喜欢
  • 2015-10-16
  • 1970-01-01
  • 2011-04-16
  • 2012-11-22
  • 2010-09-29
  • 2011-11-17
相关资源
最近更新 更多