我的错:
正如@Joop-Egen在他的回答中所说,我的问题之一是项目中的文件夹结构。我没有遵循maven的约定,将资源文件夹放在src/main/文件夹中,这是因为我尝试的所有解决方案都没有获得资源文件夹所需的作用域。
我不知道jar文件是如何与java一起工作的。对于java来说,jar文件是jar条目到其中每个文件的集合(不是java的集合)。在我的例子中,.jar文件是使用EclipseExport向导创建的,它没有任何文件夹引用,只是有文件引用。所以根本不可能得到一个包含所有内容的文件夹。
我使用java JarFile类来管理内容,但是这个类不提供像java File类那样管理文件的方法。所以这不像处理其他类型的文件那么容易。
我所做的:
我已经开发了一个代码来读取.jar中的所有文件条目,区分我感兴趣的条目。然后将它们提取到应用程序中的目录中。通过这样做,我有了对它们的标准访问权限,如果我愿意,我可以在应用程序关闭时简单地删除目录。我试图直接从jar中使用它们,但是jar文件是zip文件,因此在某个时刻,内部文件需要从jar中提取到某个地方,就像OS处理zip文件一样。它可以是一个临时目录,也可以不是。
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.apache.commons.io.FileUtils;
public class App
{
public static void main(String[] args)
{
try
{
//needed attributes
String pathToJar = "./src/main/blablalba/fileName.jar";
String folderYouWantToRetrieveFromTheJar = "/folderIWant";
String pathOfFilesWithinTheJar="src/resources/blablabla/"+folderYouWantToRetrieveFromTheJar+"/";
String tempDirectoryWhereExtract="./src/main/resources/temp";
//creating the temp directory
File tempDirectoryReference = new File(tempDirectoryWhereExtract);
if (!tempDirectoryReference.exists())
{
Files.createDirectory(tempDirectoryReference.toPath());
}
//searching what entries i need
JarFile jar = new JarFile(pathToJar);
final Enumeration entries = jar.entries();
List targetEntries = new ArrayList<>();
while (entries.hasMoreElements())
{
JarEntry entry = entries.nextElement();
//if the entry is what i need
if (entry.getName().startsWith(pathOfFilesWithinTheJar))
{
targetEntries.add(entry);
}
}
//extract every target entry from the .jar
for (JarEntry entry : targetEntries)
{
//in order to copy the structure i will get only the point where folderIWant is present
int index = entry.getName().indexOf(folderYouWantToRetrieveFromTheJar);
String newTemporaryPath = tempDirectoryReference.getPath().toString()+"/"+entry.getName().substring(index);
extractFileFromJar(jar, entry, new File(newTemporaryPath));
}
jar.close();
//(optional) clean after use
FileUtils.deleteDirectory(tempDirectoryReference);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void extractFileFromJar (JarFile jarFile, JarEntry targetEntry, File destinyPath)
{
try
{
if (!destinyPath.getParentFile().exists())
{
createFolderStructure(destinyPath);
}
else
{
Files.createFile(destinyPath.toPath());
}
InputStream inputStream = jarFile.getInputStream(targetEntry);
FileOutputStream outputStream = new java.io.FileOutputStream(destinyPath);
while (inputStream.available() > 0) {
outputStream.write(inputStream.read());
}
outputStream.flush();
outputStream.close();
inputStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
private static void createFolderStructure(File destinyPath)
{
File parentPath = destinyPath.getParentFile();
try
{
if (parentPath.exists())
{
Files.createFile(destinyPath.toPath());
}
else
{
Files.createDirectories(destinyPath.getParentFile().toPath());
Files.createFile(destinyPath.toPath());
}
}
catch(IOException e)
{
System.err.println(e.getMessage());
}
}
}
本文介绍了解决Maven项目中资源文件加载失败的问题。作者通过调整文件夹结构并编写代码来从.jar文件中提取特定文件夹内容,实现了资源的有效访问。文中提供了具体的Java实现代码。

被折叠的 条评论
为什么被折叠?



