springboot

您所在的位置:网站首页 maven打包文件名乱码 springboot

springboot

2024-07-15 23:08| 来源: 网络整理| 查看: 265

简介  这篇文章主要介绍了springboot-项目获取resources下文件碰到的问题(classPath下找不到文件和文件名乱码)以及相关的经验技巧,文章约17681字,浏览量498,点赞数7,值得推荐!

项目是spring-boot + spring-cloud 并使用maven 管理依赖。在springboot+maven项目下怎么读取resources下的文件实现文件下载?

怎么获取resources目录下的文件?(相对路径) 方法一: File sourceFile = ResourceUtils.getFile("classpath:templateFile/test.xlsx"); //这种方法在linux下无法工作 方法二: Resource resource = new ClassPathResource("templateFile/test.xlsx"); File sourceFile = resource.getFile(); 我使用的是第二种。 @PostMapping("/downloadTemplateFile") 2 public JSONData downloadTemplateFile(HttpServletResponse response) { 3 String filePath = "templateFile/test.xlsx"; 4 Resource resource = new ClassPathResource(filePath);//用来读取resources下的文件 5 InputStream is = null; 6 BufferedInputStream bis = null; 7 OutputStream os = null; 8 try { 9 File file = resource.getFile(); 10 if (!file.exists()) { 11 return new JSONData(false,"模板文件不存在"); 12 } 13 is = new FileInputStream(file); 14 os = response.getOutputStream(); 15 bis = new BufferedInputStream(is); 16 //设置响应头信息 17 response.setCharacterEncoding("UTF-8"); 18 this.response.setContentType("application/octet-stream; charset=UTF-8"); 19 StringBuffer contentDisposition = new StringBuffer("attachment; filename=""); 20 String fileName = new String(file.getName().getBytes(), "utf-8"); 21 contentDisposition.append(fileName).append("""); 22 this.response.setHeader("Content-disposition", contentDisposition.toString()); 23 //边读边写 24 byte[] buffer = new byte[500]; 25 int i; 26 while ((i = bis.read(buffer)) != -1) { 27 os.write(buffer, 0, i); 28 } 29 os.flush(); 30 } catch (FileNotFoundException e) { 31 e.printStackTrace(); 32 return new JSONData(false,"模板文件不存在"); 33 } catch (IOException e) { 34 e.printStackTrace(); 35 } finally { 36 try { 37 if(os != null) os.close(); 38 if(bis != null) bis.close(); 39 if(is != null) is.close(); 40 } catch (IOException e) { 41 e.printStackTrace(); 42 } 43 } 44 return new JSONData("模板文件下载成功"); 45 }

高高兴兴的启动项目后发现还是找不到文件,错误日志:

1 java.io.FileNotFoundException: class path resource [templateFile/test.xlsx] cannot be resolved to URL because it does not exist 2 at org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:195) 3 at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:129) 4 at com.citycloud.parking.support.controller.operate.OperateBusinessUserController.downloadTemplateFile(OperateBusinessUserController.java:215) 5 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 6 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 7 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 8 at java.lang.reflect.Method.invoke(Method.java:498) 9 at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209) 10 at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) 11 at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) 12 at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:877) 13 at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:783) 14 at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) 15 at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991) 16 at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925) 17 at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974) 18 at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:877) 19 at javax.servlet.http.HttpServlet.service(HttpServlet.java:661) 20 at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851) 21 at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) 22 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) 23 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 24 at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 25 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 26 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 27 at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.filterAndRecordMetrics(WebMvcMetricsFilter.java:158) 28 at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.filterAndRecordMetrics(WebMvcMetricsFilter.java:126) 29 at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:111) 30 at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 31 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

因为我知道Resource resource = new ClassPathResource("templateFile/test.xlsx");就是到classPath*(注意这里有个*)下去找,然后就去项目本地的目录下找classPath下是否有这个文件(maven的classPath在 “targetclasses”目录下),就发现并没有这个文件在。

后面仔细想想这是Maven项目啊,所以就找pom.xml文件去看看,突然想起:maven默认只会加载classPath同级目录下文件(配置那些),其他的需要配置标签:

1 2 3 4 src/main/java 5 6 **/*.properties 7 **/*.xml 8 9 10 false 11 12 13 src/main/resources 14 15 **/*.properties 16 **/*.xml 17 **/*.yml 18 **/Dockerfile 19 **/*.xlsx 20 21 22 false 23 24 25


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3