天空是藍(lán)色的

          做好軟件為中國(guó) #gcc -c helloworld.c -o helloworld.o //編譯目標(biāo)文件 #gcc helloworld.o -o helloworld //編譯成可執(zhí)行exe #helloworld //運(yùn)行exe

          公告


           

          常用鏈接

          留言簿(9)

          隨筆分類(149)

          隨筆檔案(151)

          文章分類(31)

          文章檔案(31)

          C編譯器

          leemay`s

          朋友的blog

          相冊(cè)

          計(jì)算機(jī)原理

          鏈接

          搜索

          •  

          積分與排名

          • 積分 - 450191
          • 排名 - 121

          最新隨筆

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          采用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 閱讀(350) 評(píng)論(0)  編輯  收藏 所屬分類: 工作總結(jié)

          主站蜘蛛池模板: 墨江| 石家庄市| 苍溪县| 峨山| 临邑县| 和静县| 阆中市| 增城市| 台南县| 贞丰县| 平凉市| 诸城市| 沂水县| 华坪县| 禄丰县| 菏泽市| 左云县| 松滋市| 安溪县| 增城市| 密山市| 龙井市| 桦南县| 赤城县| 香港| 平远县| 六安市| 雅安市| 广灵县| 邵阳县| 方山县| 南充市| 木里| 满城县| 兴国县| 河源市| 项城市| 民乐县| 香河县| 余干县| 石门县|