採用HttpServlet 實現web檔案下載

來源:互聯網
上載者:User
servlet|web|下載

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("\\");
  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);
   }
  }
 }
}



相關文章

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。