天空是藍色的

          做好軟件為中國 #gcc -c helloworld.c -o helloworld.o //編譯目標文件 #gcc helloworld.o -o helloworld //編譯成可執行exe #helloworld //運行exe
          數據加載中……
          采用HttpServlet 實現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: 困惑我與我的項目組很久的web文件下載,今天終于解決了,借此也奉獻給與我一樣的困惑的朋友</p>
           * <p>Copyright: 拓智軟件 版權所有</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轉碼
           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;
           }

           
          //取得下載文件的真實全路徑名稱
           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;
           }

           
          //實現下載
           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); 
             
          //設置輸出的格式 
             response.reset(); 
             response.setContentType(
          "application/x-msdownload"); 
             
             
             response.addHeader(
          "Content-Disposition","attachment; filename=\"" + attch_name + "\"");
             
          //循環取出流中的數據 
             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 閱讀(348) 評論(0)  編輯  收藏 所屬分類: 工作總結

          主站蜘蛛池模板: 金湖县| 伊春市| 宣威市| 梁平县| 临沂市| 皋兰县| 宁晋县| 福贡县| 吉林省| 确山县| 元阳县| 三穗县| 札达县| 八宿县| 弥渡县| 沅陵县| 来凤县| 肥西县| 淮南市| 饶阳县| 沁水县| 大同县| 江北区| 芦山县| 长丰县| 美姑县| 莲花县| 蒙自县| 临桂县| 昌图县| 咸宁市| 新平| 平泉县| 耒阳市| 左权县| 六安市| 雅安市| 青川县| 钟山县| 曲水县| 澄江县|