<%@page import="javax.servlet.*, javax.servlet.http.*,java.io.*,java.util.*,com.kit.log.*,com.kit.db.util.*"%> <%@include file="sessioncheck.jsp"%> <% System.out.println("downloadcallrecoeding jsp start...................................."); String strfileName = request.getParameter("filename"); String strfilePath = System.getProperty("callrecordingfilespath"); String strabsoluteFileName = strfilePath + strfileName + ".mp3"; String strtempfileName = request.getParameter("filename") + ".mp3"; try{ strtempfileName = strtempfileName.substring(6, strtempfileName.length()); }catch(Exception e){ } File requiredFile = new File(strabsoluteFileName); if ( requiredFile.exists()) { response.setHeader("Content-Type", "audio/mpeg; filename=" + strabsoluteFileName); response.setHeader("Content-Disposition", "attachment; filename=" + strtempfileName); response.setHeader("Content-Length", "" + requiredFile.length()); try { OutputStream os = response.getOutputStream(); FileInputStream fis = new FileInputStream(requiredFile); int ch; while ( ( ch = fis.read() ) != -1 ) { os.write(ch); os.flush(); } fis.close(); } catch ( Exception e ) { } } else { %> <script> alert( "This file does not exist." ); window.history.back(); </script> <% } System.out.println("downloadcallrecoeding jsp end......................................."); %> **Error:** ______ java.lang.IllegalStateException: getOutputStream() has already been called for this response at org.apache.catalina.connector.Response.getWriter(Response.java:599) at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:195) at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:124) at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:117) at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:191) at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:115) at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:75) at org.apache.jsp.AdminSaaS.downloadcallrecording_jsp._jspService(downloadcallrecording_jsp.java:121) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97) at javax.servlet.http.HttpServlet.service(HttpServlet.java:802) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264) at javax.servlet.http.HttpServlet.service(HttpServlet.java:802) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105) at org.apache.catalina.valves.FastCommonAccessLogValve.invoke(FastCommonAccessLogValve.java:495) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869) at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664) at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527) at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80) at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684) at java.lang.Thread.run(Thread.java:595)
We are providing you the code in jsp.
1)viewFiles.jsp:
<%@ page import="java.io.*"%> <html> <table> <tr><th>File Name</th><th>View File</th></tr> <% File f = new File("C:/UploadedFiles"); File[] files = f.listFiles(); for(int i=0;i<files.length;i++){ String name=files[i].getName(); String path=files[i].getPath(); %> <tr><td><%=name%></td><td><a href="download.jsp?f=<%=path%>">View</a></td></tr> <% } %> </table> </html>
2)download.jsp:
<%@page import="java.io.*"%> <% String ff=request.getParameter("f"); File f = new File(ff); String filename=f.getName(); response.setContentType("application/zip"); response.setHeader("Content-Disposition", "attachment; filename=\""+filename+"\""); String name = f.getName().substring(f.getName().lastIndexOf("/") + 1,f.getName().length()); InputStream in = new FileInputStream(f); ServletOutputStream outs = response.getOutputStream(); int bit = 256; int i = 0; try{ while((bit) >= 0){ bit = in.read(); outs.write(bit); } } catch (IOException ioe) { ioe.printStackTrace(System.out); } outs.flush(); outs.close(); in.close(); %>
Ads