我的控制器在Spring Boot中复制目录/resources/templates以生成一个新网站,在这个文件夹中有我需要的所有文件,html,js,css ext。 从 IDE 运行所有工作,例如路径target/classes/templates
,但在我创建包的那一刻.jar不再工作了。这是因为我使用的路径是错误的,无法访问文件夹/templates/,我尝试了很多事情,我最后一次尝试的是这个,但不起作用。
String templatePath="";
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource resource = null;
try {
resource = resolver.getResource("classpath:/templates/");
templatePath = resource.getURL().getPath();
} catch (IOException e) {
e.printStackTrace();
}
我需要字符串中的路径,以传递给此函数...
templateServices.copyDirectory(templatePath, relativePath);
public void copyDirectory(String pathSrc, String pathDest) {
File srcDir = new File(pathSrc);
File destDir = new File(pathDest);
try {
System.out.println("Directory Created");
FileUtils.copyDirectory(srcDir, destDir);
System.out.println("Template Copied");
} catch (IOException e) {
e.printStackTrace();
}
}
运行时的错误是这样的。
java.io.FileNotFoundException: Source 'file:C:UsersasdasdasdDesktopnewFolderasd.jar!BOOT-INFclasses!templates' does not exist
at org.apache.commons.io.FileUtils.checkFileRequirements(FileUtils.java:
1383)
at org.apache.commons.io.FileUtils.copyDirectory(FileUtils.java:1346)
at org.apache.commons.io.FileUtils.copyDirectory(FileUtils.java:1246)
at org.apache.commons.io.FileUtils.copyDirectory(FileUtils.java:1215)
at asd.transfer.Services.TemplateServices.copyDirectory(TemplateSer
vices.java:27)
at asd.transfer.Controller.FileController.uploadFiles(FileControlle
r.java:96)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvok
e(InvocableHandlerMethod.java:190)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeF
orRequest(InvocableHandlerMethod.java:138)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocabl
eHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingH
andlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingH
andlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapt
er.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(Dispatch
erServlet.java:1039)
at org.springframework.web.servlet.DispatcherServlet.doService(Dispatche
rServlet.java:942)
at org.springframework.web.servlet.FrameworkServlet.processRequest(Frame
workServlet.java:1005)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServ
let.java:908)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkSer
vlet.java:882)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53
)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterIntern
al(CharacterEncodingFilter.java:200)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerR
equestFilter.java:109)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(Authentica
torBase.java:490)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:92)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.jav
a:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java
:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLig
ht.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(Abstract
Protocol.java:853)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpo
int.java:1587)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBas
e.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskTh
read.java:61)
at java.lang.Thread.run(Unknown Source)
但是您的资源不是文件,因此任何将其视为文件的尝试都注定要失败。例如,如果资源不是文件,则@Shuki的注释不起作用(在您的示例中,它不是:它是 jar 文件中的一个条目)。
解决方案是获取任何需要文件的代码并修复它。你永远不应该要求一个文件;输入流或 URL(您可以从中获取输入流)是您的代码应该需要的,仅此而已。我不知道asd.transfer
是什么包,也不知道是谁写的,但他们需要修复它/你不应该使用它。
或者,如果你必须这样做,那么你就不能在这里真正使用类路径,或者你被迫将这段代码作为jar文件分发;你必须在代码工作之前解压缩它。这不是惯用的java,通常不值得麻烦。
YourClassName.class.getResource("somefile.txt")
为您提供可以使用的 URI;YourClassName.class.getResourceAsStream("somefile.txt")
为您提供要读取的输入流。这就是你应该编写代码的目的;不是文件。