servlet|web| Download
Import java.io.*;
Import javax.servlet.*;
Import javax.servlet.http.*;
Import com. topwisdom.framework.util.*;
public class Webdownload extends HttpServlet {
Public Webdownload () {
}
Private ServletConfig config;
public void init (ServletConfig config) throws servletexception {
Super.init (config);
this.config = config;
}
public void DoPost (HttpServletRequest req,httpservletresponse res) throws Servletexception {
Doget (Req,res);
}
file://get the name of the attachment
public static string Getattachname (string file_name) {
if (file_name==null) return "";
file_name = File_name.trim ();
int iPos = 0;
IPos = file_name.lastindexof ("\ \");
if (ipos>-1) {
file_name = file_name.substring (ipos+1);
}
IPos = File_name.lastindexof ("/");
if (ipos>-1) {
file_name = file_name.substring (ipos+1);
}
IPos = File_name.lastindexof (file.separator);
if (ipos>-1) {
file_name = file_name.substring (ipos+1);
}
return file_name;
}
File://UTF8 Transfer Code
public static string toutf8string (string s) {
StringBuffer sb = new StringBuffer ();
for (int i=0;i<s.length (); i++) {
char C = S.charat (i);
if (c >= 0 && C <= 255) {
Sb.append (c);
} else {
Byte[] B;
try {
b = character.tostring (c). GetBytes ("Utf-8");
catch (Exception ex) {
System.out.println (ex);
b = new Byte[0];
}
for (int j = 0; J < B.length; J + +) {
int k = B[j];
if (k < 0) k = 256;
Sb.append ("%" + integer.tohexstring (k). toUpperCase ());
}
}
}
String S_utf8 = sb.tostring ();
Sb.delete (0,sb.length ());
Sb.setlength (0);
SB = null;
return S_utf8;
}
file://Obtain the true full path name of the downloaded file
Private String Getrealname (httpservletrequest request,string file_name) {
if (Request==null | | file_name==null) return NULL;
file_name = File_name.trim ();
if (File_name.equals ("")) return null;
String File_path = Request.getrealpath (file_name);
if (file_path== null) return null;
File File = new file (File_path);
if (!file.exists ()) return null;
return file_path;
}
file://implementation Download
public void doget (HttpServletRequest request,httpservletresponse response) throws Servletexception {
String file_name = Request.getparameter ("file_name");
if (file_name==null) file_name = "";
file_name = File_name.trim ();
InputStream instream= null;
String attch_name = "";
Byte[] B = new byte[100];
int len= 0;
try {
file://get the name of the attachment
Attch_name = Getattachname (file_name);
file_name = Getrealname (request,file_name);
if (file_name==null) {
SYSTEM.OUT.PRINTLN ("File does not exist, or prohibit downloading");
return;
}
Attch_name = toutf8string (attch_name);
file://read into the stream
Instream=new FileInputStream (file_name);
file://format of output
Response.reset ();
Response.setcontenttype ("Application/x-msdownload");
Response.AddHeader ("Content-disposition", "attachment; filename=\" "+ attch_name +" \ ");
file://loop to remove data from the stream
while ((Len=instream.read (b)) >0) {
Response.getoutputstream (). write (B,0,len);
}
Instream.close ();
}catch (Exception e) {
if (e instanceof java.io.FileNotFoundException) {
try {
Response.sendredirect ("/tip/file_not_found.html");
}
catch (IOException ex) {
Ex.printstacktrace (System.err);
}
}
else {
E.printstacktrace (System.err);
}
}
}
}