java generic file Download Excel,pdf,word,jpg,exe,rar

Source: Internet
Author: User

<%@ 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.