Ver Mensaje Individual
  #7 (permalink)  
Antiguo 03/01/2007, 11:02
Langas
 
Fecha de Ingreso: enero-2007
Mensajes: 17
Antigüedad: 17 años, 4 meses
Puntos: 0
Re: IllegalStateException en JSP

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import java.sql.*;
import java.util.zip.GZIPOutputStream;
import java.io.*;

public class servletDescarga extends HttpServlet {

PageContext pageContext;
private static final int BUFFER_SIZE = 4096;
private boolean compress = false;
private boolean inline = false;
private String file = null;
private String dir = null;
private String contentType = null;

public void setCompress(boolean flag)
{
compress = flag;
}

public void setCompress(String s)
{
if("true".equals(s))
{
compress = true;
} else
{
compress = false;
}
}

public boolean getCompress()
{
return compress;
}

public void setInline(boolean flag)
{
inline = flag;
}

public void setInline(String s)
{
if("true".equals(s))
{
inline = true;
} else
{
inline = false;
}
}

public boolean getInline()
{
return inline;
}

public void setFile(String s)
{
file = s;
}

public String getFile()
{
return file;
}

public void setDir(String s)
{
dir = s;
}

public String getDir()
{
return dir;
}

public void setContentType(String s)
{
contentType = s;
}

public String getContentType()
{
return contentType;
}

public int doEndTag()
throws JspException
{
HttpServletRequest httpservletrequest = (HttpServletRequest)pageContext.getRequest();
HttpServletResponse httpservletresponse = (HttpServletResponse)pageContext.getResponse();
String s;
if(dir == null)
{
s = "";
} else
{
s = dir;
String s1 = System.getProperty("file.separator");
if(!s.endsWith(s1) && !s.endsWith("/") && !file.startsWith(s1) && !file.startsWith("/"))
{
s = s + s1;
} else
if((s.endsWith(s1) || s.endsWith("/")) && (file.startsWith(s1) || file.startsWith(s1)))
{
s = s.substring(0, s.length() - 1);
}
}
s = s + file;
if(!compress)
{
sendFile(s, httpservletresponse);
} else
if(httpservletrequest.getHeader("Accept-Encoding").indexOf("gzip") < 0)
{
sendFile(s, httpservletresponse);
} else
{
sendCompressed(s, httpservletresponse);
}
return 6;
}

private void sendCompressed(String s, HttpServletResponse httpservletresponse)
throws JspException
{
String s1 = System.getProperty("file.separator");
try
{
pageContext.getOut().clearBuffer();
}
catch(Exception exception) { }
int i = s.lastIndexOf(s1);
if(i < 0 && s1.equals("\\"))
{
i = s.lastIndexOf("/");
}
String s2;
if(i > 0 && i != s.length() - 1)
{
s2 = s.substring(i + 1);
} else
{
s2 = s;
}
httpservletresponse.setContentType(getContentType( s2) + "; name=" + s2 + "");
httpservletresponse.setHeader("Content-Encoding", "gzip");
if(!inline)
{
httpservletresponse.setHeader("Content-Disposition", "attachment;filename=\"" + s2 + "\"");
} else
{
httpservletresponse.setHeader("Content-Disposition", "inline;filename=\"" + s2 + "\"");
}
javax.servlet.ServletOutputStream servletoutputstream;
GZIPOutputStream gzipoutputstream;
try
{
servletoutputstream = httpservletresponse.getOutputStream();
gzipoutputstream = new GZIPOutputStream(servletoutputstream);
}
catch(Exception exception1)
{
throw new JspException("Could not get OutputStream");
}
if(!dumpFile(s, gzipoutputstream))
{
throw new JspException("Could not download file");
}
try
{
gzipoutputstream.close();
servletoutputstream.flush();
servletoutputstream.close();
}
catch(Exception exception2) { }
}

private void sendFile(String s, HttpServletResponse httpservletresponse)
throws JspException
{
String s1 = System.getProperty("file.separator");
try
{
pageContext.getOut().clearBuffer();
}
catch(Exception exception) { }
int i = s.lastIndexOf(s1);
if(i < 0 && s1.equals("\\"))
{
i = s.lastIndexOf("/");
}
String s2;
if(i > 0 && i != s.length() - 1)
{
s2 = s.substring(i + 1);
} else
{
s2 = s;
}
httpservletresponse.setContentType(getContentType( s2) + "; name=" + s2 + "");
if(!inline)
{
httpservletresponse.setHeader("Content-Disposition", "attachment;filename=\"" + s2 + "\"");
} else
{
httpservletresponse.setHeader("Content-Disposition", "inline;filename=\"" + s2 + "\"");
}
javax.servlet.ServletOutputStream servletoutputstream;
try
{
servletoutputstream = httpservletresponse.getOutputStream();
}
catch(Exception exception1)
{
throw new JspException("Could not get OutputStream: " + exception1.getMessage());
}
if(!dumpFile(s, servletoutputstream))
{
throw new JspException("Could not download file");
}
try
{
servletoutputstream.flush();
servletoutputstream.close();
}
catch(Exception exception2) { }
}

private boolean dumpFile(String s, OutputStream outputstream)
{
byte abyte0[] = new byte[4096];
boolean flag = true;
try
{
FileInputStream fileinputstream = new FileInputStream(lookupFile(s));
int i;
while((i = fileinputstream.read(abyte0)) != -1)
{
outputstream.write(abyte0, 0, i);
}
fileinputstream.close();
}
catch(Exception exception)
{
flag = false;
}
return flag;
}