1. 程式人生 > >spring4mvc 實現檔案下載功能reponse.getOutputStream()重複呼叫問題解決

spring4mvc 實現檔案下載功能reponse.getOutputStream()重複呼叫問題解決

本來是準備做一個類似網路硬碟的功能,判斷目標是檔案的時候,點選就啟動下載。

誰想遇到了不少麻煩,最直接的麻煩就是遇到了response.getOutputStream()重複使用的異常。。雖然對功能沒有影響,但是這個報錯的異常始終看著不舒服,百度上搜索了一大圈,總是說什麼getWriter()和getOutputStream()只能二選一,因為我採用的是spring4管理,在JSP沒有任何jsp程式碼片段(實際上,就算是我加入了out.clean()兩行程式碼也無效)。

其實很簡單,只是自己以前沒有用spring框架的時候也遇到過,就是return null;就搞定了。

異常如下

八月 11, 2014 8:33:31 上午 org.apache.catalina.core.StandardWrapperValve invoke
嚴重: Servlet.service() for servlet [xchgx] in context with path [/HbouCn] threw exception [java.lang.IllegalStateException: getOutputStream() has already been called for this response] with root cause
java.lang.IllegalStateException: getOutputStream() has already been called for this response
	at org.apache.catalina.connector.Response.getWriter(Response.java:636)
	at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:213)
	at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:104)
	at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
	at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
	at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:186)
	at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:126)
	at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:80)
	at org.apache.jsp.WEB_002dINF.views.manager.disk.main_jsp._jspService(main_jsp.java:166)
	at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
	at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748)
	at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:604)
	at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:543)
	at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:201)
	at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:267)
	at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1221)
	at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1005)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:952)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:745)

出錯的程式碼:
	/**
	 * 根據type判斷為檔案,就啟動下載
	 * @param path 檔名
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value="next",method=RequestMethod.GET,params = "type=1")
	public ModelAndView download(@RequestParam(value="path") String path,HttpServletRequest request,HttpServletResponse response) throws IOException{
//這裡要轉換,不論你是否在tomcat下的server.xml做過修改,還是在JSP中加入response.setCharacter("UTF-8");
//或request.....UTF-8都沒用,包括spring-servlet.xml和web.xml加入encoding utf-8過濾器都無效,這裡一定要加這一句話才能識別中文。
<pre name="code" class="java">		String nPath = new String(path.getBytes("ISO-8859-1"),"UTF-8");
                String parentPath = (String) getSessionObject(request, CommonConstant.CURRENT_DIRECTORY);
		String filePath = diskService.getNextDirectory(parentPath, nPath);
		diskService.download(filePath,request,response);
		
		return new ModelAndView("manager/disk/main");
	}
	
最後決定就加了一句話就搞定了。害我折騰了2天。
	/**
	 * 根據type判斷為檔案,就啟動下載
	 * @param path 檔名
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value="next",method=RequestMethod.GET,params = "type=1")
	public ModelAndView download(@RequestParam(value="path") String path,HttpServletRequest request,HttpServletResponse response) throws IOException{
		String nPath = new String(path.getBytes("ISO-8859-1"),"UTF-8");
		String parentPath = (String) getSessionObject(request, CommonConstant.CURRENT_DIRECTORY);
		String filePath = diskService.getNextDirectory(parentPath, nPath);
		diskService.download(filePath,request,response);
		
		<span style="color:#FF0000;">return null;</span>//媽的原來返回一個null就搞定了response重複呼叫的問題,害死我了。
	}
	


做個筆記,防止忘記