wap页面下载

对于wap页面下载软件记录下
URL要带文件名,否则手机不支持
例如:http://localhost:8080/client/test.CAB
symbian手机对于IP:8080/方式貌似不支持,需要http://域名:端口/client/test.CAB


下载方式可以有2种:
1)wap标签连接
<wall:a href="<%=filePath%>">download</wall:a>
2)servlet write文件流返回(字节流)
配置一个DownLoadServlet
web.xml:
<servlet>
        <servlet-name>downLoadClientServlet</servlet-name>
        <servlet-class>com.test.DownLoadServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
        <servlet-name>downLoadClientServlet</servlet-name>
        <url-pattern>/client/*</url-pattern>
</servlet-mapping>
DownLoadServlet:

Java代码 复制代码
  1. public class DownLoadServlet extends HttpServlet {   
  2.     private GeneralLogger logger = LoggerFactory.getLogger(DownLoadServlet.class);   
  3.   
  4.     public void doGet(HttpServletRequest request, HttpServletResponse response)   
  5.             throws ServletException, IOException {   
  6.         doPost(request, response);   
  7.     }   
  8.   
  9.     public void doPost(HttpServletRequest request, HttpServletResponse response)   
  10.             throws ServletException, IOException {   
  11.            
  12.         logger.debug("------------------------------------------------------------------------------------");   
  13.   
  14.         try {                 
  15.                 String filePath = "d:/mytest/client/test.CAB";   
  16.   
  17.                 logger.debug("filePath = " + filePath);   
  18.   
  19.                 sendReply(filePath, response);   
  20.   
  21.         } catch (Exception e) {   
  22.             logger.error("error :" + e.toString());   
  23.         }   
  24.         logger.debug("------------------------------------------------------------------------------------");   
  25.     }   
  26.   
  27.     public void sendReply(String filePath, HttpServletResponse response) {          
  28.         File file = new File(filePath);   
  29.         if (file.exists()) {   
  30.             String fileName = file.getName();   
  31.             response.setContentType("application/x-download");//设置为下载application/x-download   
  32.   
  33.             response.addHeader("Content-Disposition""attachment;filename=" + fileName);   
  34.   
  35.             OutputStream output = null;   
  36.             FileInputStream fis = null;   
  37.             try {   
  38.                 output = response.getOutputStream();   
  39.                 fis = new FileInputStream(filePath);   
  40.   
  41.                 byte[] b = new byte[1024];   
  42.                 int i = 0;   
  43.   
  44.                 while ((i = fis.read(b)) != -1) {   
  45.                     output.write(b, 0, i);   
  46.                 }   
  47.                 output.flush();   
  48.                 b = null;   
  49.             }   
  50.             catch (Exception e) {   
  51.                 logger.error("error :" + e.toString());   
  52.             }   
  53.             finally {   
  54.                 if (fis != null) {   
  55.                     try {   
  56.                         fis.close();   
  57.                     } catch (IOException e) {   
  58.                         //e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.   
  59.                     }   
  60.                     fis = null;   
  61.                 }   
  62.                 if (output != null) {   
  63.                     try {   
  64.                         output.close();   
  65.                     } catch (IOException e) {   
  66.                         //e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.   
  67.                     }   
  68.                     output = null;   
  69.                 }   
  70.             }   
  71.         }   
  72.     }   
  73.   
  74. }  
public class DownLoadServlet extends HttpServlet {
    private GeneralLogger logger = LoggerFactory.getLogger(DownLoadServlet.class);

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        logger.debug("------------------------------------------------------------------------------------");

        try {              
                String filePath = "d:/mytest/client/test.CAB";

                logger.debug("filePath = " + filePath);

                sendReply(filePath, response);

        } catch (Exception e) {
            logger.error("error :" + e.toString());
        }
        logger.debug("------------------------------------------------------------------------------------");
    }

    public void sendReply(String filePath, HttpServletResponse response) {       
        File file = new File(filePath);
        if (file.exists()) {
            String fileName = file.getName();
            response.setContentType("application/x-download");//设置为下载application/x-download

            response.addHeader("Content-Disposition", "attachment;filename=" + fileName);

            OutputStream output = null;
            FileInputStream fis = null;
            try {
                output = response.getOutputStream();
                fis = new FileInputStream(filePath);

                byte[] b = new byte[1024];
                int i = 0;

                while ((i = fis.read(b)) != -1) {
                    output.write(b, 0, i);
                }
                output.flush();
                b = null;
            }
            catch (Exception e) {
                logger.error("error :" + e.toString());
            }
            finally {
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        //e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                    }
                    fis = null;
                }
                if (output != null) {
                    try {
                        output.close();
                    } catch (IOException e) {
                        //e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                    }
                    output = null;
                }
            }
        }
    }

}



顺便说下,第一种方式软件要放在web文件夹或子文件夹下,否则tomcat等容器是不允许访问的
第二种方式就比较灵活些,对于手机客户端软件升级,后台连接,可采用第二种方式。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值