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://取得附件的名稱
 public static string getattachname(string file_name) {
  if(file_name==null) return "";
  file_name = file_name.trim();
  int ipos = 0;
  ipos = file_name.lastindexof("http://");
  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轉碼
 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://取得下載文件的真實全路徑名稱
 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://實現下載
 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://取得附件的名稱
   attch_name = getattachname(file_name);
   
   file_name  = getrealname(request,file_name);
   if(file_name==null) {
    system.out.println("文件不存在,或者禁止下載");
    return ;
   }
   attch_name = toutf8string(attch_name);
   file://讀到流中
   instream=new fileinputstream(file_name); 
   file://設置輸出的格式 
   response.reset(); 
   response.setcontenttype("application/x-msdownload"); 
   
   
   response.addheader("content-disposition","attachment; filename=/"" + attch_name + "/"");
   file://循環取出流中的數據 
   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);
   }
  }
 }
}
新聞熱點
疑難解答