<%@ page contenttype= "text/html; CHARSET=GBK "%>
<%@ page autoflush= "false"%>
<%@ page import= "java.sql.*"%>
<%@ page import= "java.io.*"%>
<%@ page import= "java.util.*"%>
<%@ page import= "java.awt.*"%>
<%@ page import= "java.awt.image.*"%>
<%@ page import= "com.sun.image.codec.jpeg.*"%>
<%@ page import= "com.sun.image.codec.jpeg.*"%>
<%!
public static string getContentType (String pextfilename)//Get extension by gisoracle {
String pext=pextfilename.tolowercase ();
if (Pext.equals (". xls"))
{
return "application/vnd.ms-excel";
}
if (Pext.equals (". Doc"))
{
return "Application/msword";
}
if (Pext.equals (". txt"))
{
return "Text/plain";
}
if (Pext.equals (". pdf"))
{
return "Application/pdf";
}
if (Pext.equals (". jpg") | | Pext.equals (". jpeg"))
{
return "Image/jpeg";
}
if (Pext.equals (". ppt"))
{
return "Application/vnd.ms-powerpoint";
}
if (Pext.equals (". gif"))
{
return "Image/gif";
}
return "text/html";
}
%>
<%
String filename =request.getparameter ("filename");
File Downfile = new file (filename);
String Fileext=fileoper.getfiletype (Downfile);
System.out.println ("fileext=" +fileext);
String contentType1 =getcontenttype (fileext);
System.out.println ("contenttype1=" +contenttype1);
try {
Out.clear ();
Response.setcontenttype (CONTENTTYPE1);
filename = new String (filename.getbytes ("GBK"), "iso-8859-1");
Response.setheader ("Content-disposition", "attachment; Filename=" +downfile.getname ());
Response.AddHeader ("Cache-control", "No-cache");
InputStream Blobstream = new FileInputStream (downfile);
Servletoutputstream OutStream = Response.getoutputstream ();
byte[] buffer = new BYTE[10 * 1024];
int nbytes = 0;
while ((nbytes = blobstream.read (buffer))! =-1) {
Outstream.write (buffer, 0, nbytes);
}
Outstream.flush ();
Outstream.close ();
Blobstream.close ();
}
catch (Exception e) {
System.out.println (e);
}
%>
java generic file Download Excel,pdf,word,jpg,exe,rar