springboot打jar包之后下载文件的路径问题

 更新时间:2020年07月17日 08:31:07   作者:胖大鑫  
这篇文章主要介绍了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)
    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.doInvoke(InvocableHandlerMethod.java:205)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
    at org.springframework.web.servlet.mvc.method.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.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
    at org.apache.catalina.core.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.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
    at org.springframework.boot.actuate.trace.WebRequestTraceFilter.doFilterInternal(WebRequestTraceFilter.java:108)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
    at com.haiyisoft.cloud.web.filter.MenuRightItemFilter.doFilter(MenuRightItemFilter.java:98)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
    at com.haiyisoft.cloud.web.filter.AuthenticationFilter.doFilter(AuthenticationFilter.java:198)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
    at com.haiyisoft.cloud.web.filter.XssRefererFilter.doFilter(XssRefererFilter.java:63)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
    at com.haiyisoft.cloud.web.filter.XssSpeCharacterFilter.doFilter(XssSpeCharacterFilter.java:66)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
    at org.apache.catalina.core.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.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
    at org.apache.catalina.core.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.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
    at org.apache.catalina.core.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.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
    at org.apache.catalina.core.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.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:474)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:798)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1434)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.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(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 File(ResourceUtils.getURL("classpath:static").getPath().replace("%20"," ").replace('/', '\\') + "\\securityParameter\\template\\网络安全台账(模板).xlsx");
 byte[] body = null;
 InputStream is = new FileInputStream(file);
 body = new byte[is.available()];
 is.read(body);
 HttpHeaders headers = new HttpHeaders();
 String downlaodFilename = URLEncoder.encode(file.getName(),"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 File(ResourceUtils.getURL("classpath:static").getPath().replace("%20"," ").replace('/', '\\') + "\\securityParameter\\template\\网络安全台账(模板).xlsx");
 ClassPathResource resource = new ClassPathResource("\\static\\securityParameter\\template\\网络安全台账(模板).xlsx");
 InputStream is = resource.getInputStream();
 byte[] body = null;
 body = new byte[is.available()];
 is.read(body);
 HttpHeaders headers = new HttpHeaders();
 String downlaodFilename = URLEncoder.encode("网络安全台账(模板).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包路径问题内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java中堆和栈的概念和区别

    Java中堆和栈的概念和区别

    Java的堆是一个运行时数据区,类的对象从堆中分配空间。栈中主要存放一些基本数据类型的变量(byte,short,int,long,float,double,boolean,char)和对象的引用,这篇文章给大家详细介绍java 堆和栈的概念和区别,一起看看吧
    2020-06-06
  • java实现文件保存到本地的方法

    java实现文件保存到本地的方法

    本篇文章主要介绍了java实现文件保存到本地的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-02-02
  • feign调用中文参数被encode编译的问题

    feign调用中文参数被encode编译的问题

    这篇文章主要介绍了feign调用中文参数被encode编译的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • java读取txt文件代码片段

    java读取txt文件代码片段

    这篇文章主要为大家详细介绍了java读取txt文件的代码片段,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-05-05
  • 使用java模拟简单的tomcat的方法详解

    使用java模拟简单的tomcat的方法详解

    这篇文章主要为大家详细介绍了java模拟简单的tomcat的方法,使用数据库,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02
  • IDEA .iml文件及.idea文件夹超详细讲解

    IDEA .iml文件及.idea文件夹超详细讲解

    iml文件是IntelliJ IDEA自己创建的模块文件,用于java应用开发,存储一些模块相关的信息,比如一个Java组建,插件组建,Maven组建等,这篇文章主要介绍了IDEA .iml文件及.idea文件夹,需要的朋友可以参考下
    2023-09-09
  • SpringAOP实现自定义接口权限控制

    SpringAOP实现自定义接口权限控制

    本文主要介绍了SpringAOP实现自定义接口权限控制,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-11-11
  • SpringBoot中定义Bean的方法总结

    SpringBoot中定义Bean的方法总结

    在Spring Boot应用程序中,定义Bean是非常常见的操作,它是构建应用程序的基础,pring Boot提供了多种方式来定义Bean,每种方式都有其适用的场景和优势,本文将介绍Spring Boot中定义Bean的几种常见方式,需要的朋友可以参考下
    2023-12-12
  • Java代码执行shell命令的实现

    Java代码执行shell命令的实现

    这篇文章主要介绍了Java代码执行shell命令的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-09-09
  • java fastjson传输long数据却接收到了int的问题

    java fastjson传输long数据却接收到了int的问题

    这篇文章主要介绍了java fastjson传输long数据却接收到了int的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01

最新评论