springboot打jar包之后下载⽂件的路径问题
错误信息
能看到下载的路径中包含了jar包,wlaqWeb-0.0.1-SNAPSHOT.jar!
java.io.FileNotFoundException: file:\C:\Users\PENGXIN\Desktop\⽹络安全\jars\wlaqWeb-0.0.1-SNAPSHOT.jar!\BOOT-INF\classes!\static\securityParameter\template\⽹络安全台账(模板).xlsx (⽂件名、⽬录名或卷标语法不正确。) at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at com.haiyisoft.wlaq.wlaqWeb.ui.securityParameter.BnsBookEditWebController.downloadTemplate(BnsBookEditWebController.java:509)
flect.NativeMethodAccessorImpl.invoke0(Native Method)
flect.NativeMethodAccessorImpl.invoke(Unknown Source)
flect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at flect.Method.invoke(Unknown Source)
at org.hod.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
at org.hod.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
at org.springframework.web.hod.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116)
at org.springframework.web.hod.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.hod.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.hod.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
at org.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at at.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.springframework.ace.WebRequestTraceFilter.doFilterInternal(WebRequestTraceFilter.java:108)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at com.haiyisoft.cloud.web.filter.MenuRightItemFilter.doFilter(MenuRightItemFilter.java:98)
at org.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at com.haiyisoft.cloud.web.filter.AuthenticationFilter.doFilter(AuthenticationFilter.java:198)
at org.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at com.haiyisoft.cloud.web.filter.XssRefererFilter.doFilter(XssRefererFilter.java:63)
at org.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at com.haiyisoft.cloud.web.filter.XssSpeCharacterFilter.doFilter(XssSpeCharacterFilter.java:66)
at org.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
spring怎么读取jar文件
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:105)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.springframework.boot.actuate.autoconfigure.MetricsFilter.doFilterInternal(MetricsFilter.java:106)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:474)
at org.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.tor.CoyoteAdapter.service(CoyoteAdapter.java:349)
at http11.Http11Processor.service(Http11Processor.java:783)
at AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:798)
at at.util.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1434)
at at.util.SocketProcessorBase.run(SocketProcessorBase.java:49)
at urrent.ThreadPoolExecutor.runWorker(Unknown Source)
at urrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at at.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
后台代码
/**
* 模板下载
* @param request
* @return
*/
@RequestMapping("/downloadTemplate")
public ResponseEntity<byte[]> downloadTemplate(HttpServletRequest request){
ResponseEntity<byte[]> entity = null;
try {
File file = new URL("classpath:static").getPath().replace("%20"," ").replace('/', '\\') + "\\securityParameter\\template\\⽹络安全台账(模板).xlsx");
byte[] body = null;
InputStream is = new FileInputStream(file);
body = new byte[is.available()];
HttpHeaders headers = new HttpHeaders();
String downlaodFilename = Name(),"utf-8");
headers.add("Content-Disposition", "attchement;filename=" + downlaodFilename);
headers.add("Content-Type", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
HttpStatus statusCode = HttpStatus.OK;
entity = new ResponseEntity<byte[]>(body, headers, statusCode);
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return entity;
}
修改之后的代码
/**
* 模板下载
* @param request
* @return
*/
@RequestMapping("/downloadTemplate")
public ResponseEntity<byte[]> downloadTemplate(HttpServletRequest request){
ResponseEntity<byte[]> entity = null;
try {
//File file = new URL("classpath:static").getPath().replace("%20"," ").replace('/', '\\') + "\\securityParameter\\template\\⽹络安全台账(模板).xlsx");
ClassPathResource resource = new ClassPathResource("\\static\\securityParameter\\template\\⽹络安全台账(模板).xlsx");
InputStream is = InputStream();
byte[] body = null;
body = new byte[is.available()];
HttpHeaders headers = new HttpHeaders();
String downlaodFilename = de("⽹络安全台账(模板).xlsx","utf-8");
headers.add("Content-Disposition", "attchement;filename=" + downlaodFilename);
headers.add("Content-Type", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
HttpStatus statusCode = HttpStatus.OK;
entity = new ResponseEntity<byte[]>(body, headers, statusCode);
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return entity;
}
结果如下:
到此这篇关于springboot打jar包之后下载⽂件的路径问题的⽂章就介绍到这了,更多相关springboot打jar包路径问题内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论