文件上传中的临时上传路径问题
在用multipart上传文件的时候,报错:
java.io.IOException: The temporary upload location [D:\apache-tomcat-8.0.9\work\Catalina\localhost\spittr\tmp\spittr\uploads] is not valid
at org.apache.catalina.connector.Request.parseParts(Request.java:2616)
at org.apache.catalina.connector.Request.getParts(Request.java:2552)
at org.apache.catalina.connector.RequestFacade.getParts(RequestFacade.java:1083)
at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.<init>(StandardMultipartHttpServletRequest.java:57)
at org.springframework.web.multipart.support.StandardServletMultipartResolver.resolveMultipart(StandardServletMultipartResolver.java:60)
at org.springframework.web.servlet.DispatcherServlet.checkMultipart(DispatcherServlet.java:1070)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:912)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:526)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:655)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:277)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2381)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2370)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:744)
原因:
protected void customizeRegistration(Dynamic registration) { registration.setMultipartConfig(new MultipartConfigElement("/tmp/spittr/uploads",2097152,4194304,0)); }
在这里设置了临时存储的路径,但是在tomcat中并没有设置。(之所以设置临时路径,是为了存储的安全性)
解决办法:
在D:\apache-tomcat-8.0.9\work\Catalina\localhost\spittr目录下新建tmp\spittr\uploads即可
但是如果重新启动tomcat的话,则临时保存的文件消失
为了达到永久保存,需要保存在磁盘上,但是此时会存在无法再网页上访问的问题:
这是需要:
解决:可以使用tomcat的配置文件将某个请求 映射到 物理路径下 ,完成图片的回显。
具体操作:使用Tomcat虚拟路径
1.修改tomcat的配置文件
window环境
首先找到tomcat目录下conf目录下的server.xml文件
在server.xml文件中找到<Host></Host>
然后在其中加上这例如这个:
<Context path="/spittr/image" docBase="E:\spittr\image"></Context>
tomcat在的请求一般为http:
//localhost:8080/spittr/image/abc.jpg