<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    天空是藍(lán)色的

    做好軟件為中國 #gcc -c helloworld.c -o helloworld.o //編譯目標(biāo)文件 #gcc helloworld.o -o helloworld //編譯成可執(zhí)行exe #helloworld //運(yùn)行exe
    數(shù)據(jù)加載中……
    采用HttpServlet 實(shí)現(xiàn)web文件下載
    package com.TopWisdom.framework.web;
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;

    import com.TopWisdom.framework.util.*;

    /**
     * <p>Title: tomcat 5.59文件下載</p>
     * <p>Description: 困惑我與我的項(xiàng)目組很久的web文件下載,今天終于解決了,借此也奉獻(xiàn)給與我一樣的困惑的朋友</p>
     * <p>Copyright: 拓智軟件 版權(quán)所有</p>
     * <p>Company: 拓智軟件(TopWisdom)</p>
     * 
    @author 李光明
     * 
    @version 1.0
     
    */

    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);
     }

     
    //取得附件的名稱
     public static String getAttachName(String file_name) {
      
    if(file_name==nullreturn "";
      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;
     }

     
    //UTF8轉(zhuǎn)碼
     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;
     }

     
    //取得下載文件的真實(shí)全路徑名稱
     private String getRealName(HttpServletRequest request,String file_name) {
      
    if(request==null || file_name==nullreturn null;
      file_name 
    = file_name.trim();
      
    if(file_name.equals("")) return null;
      
      String file_path 
    = request.getRealPath(file_name);
      
    if ( file_path== nullreturn null;
      File file 
    = new File(file_path);
      
    if (!file.exists()) return null;
      
    return file_path;
     }

     
    //實(shí)現(xiàn)下載
     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 {
       
    //取得附件的名稱
       attch_name = getAttachName(file_name);
       
       file_name  
    = getRealName(request,file_name);
       
    if(file_name==null{
        System.out.println(
    "文件不存在,或者禁止下載");
        
    return ;
       }

       attch_name 
    = toUtf8String(attch_name);
       
    //讀到流中
       inStream=new FileInputStream(file_name); 
       
    //設(shè)置輸出的格式 
       response.reset(); 
       response.setContentType(
    "application/x-msdownload"); 
       
       
       response.addHeader(
    "Content-Disposition","attachment; filename=\"" + attch_name + "\"");
       
    //循環(huán)取出流中的數(shù)據(jù) 
       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);
       }

      }

     }

     

    }



    作者Blog:http://blog.csdn.net/gmlee2004/

    posted on 2005-11-24 10:47 bluesky 閱讀(346) 評(píng)論(0)  編輯  收藏 所屬分類: 工作總結(jié)

    主站蜘蛛池模板: 四虎成人免费观看在线网址 | 亚洲日本在线看片| 一区二区免费在线观看| 亚洲AV网站在线观看| 猫咪免费观看人成网站在线| 日日操夜夜操免费视频| 黄页网址大全免费观看12网站| 免费大香伊蕉在人线国产| 成人免费夜片在线观看| 亚洲午夜日韩高清一区| 成人片黄网站色大片免费观看APP| 亚洲av永久无码精品秋霞电影影院| 3344在线看片免费| 亚洲视频在线观看网址| 四虎永久在线精品免费观看视频| 精品亚洲456在线播放| 国产伦一区二区三区免费| 一级片在线免费看| 久久国产亚洲精品麻豆| 免费一级毛片无毒不卡| 亚洲免费观看在线视频| 国产区卡一卡二卡三乱码免费| 色妞www精品视频免费看| 国产亚洲一区二区在线观看| 91九色老熟女免费资源站| 亚洲av无码偷拍在线观看| 亚洲人成在线播放网站| 黄色免费网站网址| 香蕉视频免费在线播放| 久久亚洲免费视频| 好爽…又高潮了毛片免费看| 日本一区二区三区在线视频观看免费| 亚洲av无码国产精品色午夜字幕| 美女视频黄的全免费视频| 午夜肉伦伦影院久久精品免费看国产一区二区三区 | 亚洲大片免费观看| 精品亚洲成A人在线观看青青| 亚洲成A人片在线观看无码不卡| 欧洲黑大粗无码免费| 中文字幕在线免费播放| 亚洲kkk4444在线观看|