我在Spring Boot中的控制器复制目录/ resources / templates以生成一个新的网站,在此文件夹中,我需要的所有文件都是html,js,css ext 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:\Users\asdasdasd\Desktop\newFolder\asd.jar!\BOOT-INF\classes!\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)
答案 0 :(得分:1)
但是您的资源不是文件,因此任何将其视为一个资源的尝试都注定会失败。例如,如果资源不是文件(在您的示例中不是:这是jar文件中的一个条目),则@Shuki的注释将不起作用。
解决方案是采用任何需要文件的代码并对其进行修复。您不应该要求文件;输入流或URL(您可以从中获取输入流)是代码所需要的,仅此而已。我不知道asd.transfer
是什么包,也不是谁写的,但是他们需要修复它/您不应该使用它。
或者,如果必须的话,那么您就不能在这里真正使用类路径,或者您不得不将这段代码而不是作为jar文件分发;您必须先解压缩它,然后才能使用代码。这不是惯用的Java,通常也不值得麻烦。
YourClassName.class.getResource("somefile.txt")
为您提供了可以使用的URI; YourClassName.class.getResourceAsStream("somefile.txt")
为您提供了一个输入流以供阅读。那就是您应该编写代码的目的;不是文件。