Download excel, PDF, word, JPG, EXE, and RAR for general java files

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) // obtain the 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 = filetings. 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 );
}
%>

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.