论坛首页 移动开发技术论坛

wap 软件下载实现

浏览 7387 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-11-18   最后修改:2009-11-18
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:
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等容器是不允许访问的
第二种方式就比较灵活些,对于手机客户端软件升级,后台连接,可采用第二种方式。
   发表时间:2009-12-11  
手机下载MP3用输出流的形式,用户名总是乱码.郁闷.
也转编码格式了.各手机表现都不一样,有的直接是UTF-8编码的文件名.有的不识别头部信息.直接文件名是一个请求名
0 请登录后投票
   发表时间:2010-01-26   最后修改:2010-01-26
wq13480 写道
手机下载MP3用输出流的形式,用户名总是乱码.郁闷.
也转编码格式了.各手机表现都不一样,有的直接是UTF-8编码的文件名.有的不识别头部信息.直接文件名是一个请求名
0 请登录后投票
   发表时间:2010-01-26   最后修改:2010-01-26
wq13480 写道
手机下载MP3用输出流的形式,用户名总是乱码.郁闷.
也转编码格式了.各手机表现都不一样,有的直接是UTF-8编码的文件名.有的不识别头部信息.直接文件名是一个请求名
0 请登录后投票
   发表时间:2010-01-26   最后修改:2010-01-26
wq13480 写道
手机下载MP3用输出流的形式,用户名总是乱码.郁闷.
也转编码格式了.各手机表现都不一样,有的直接是UTF-8编码的文件名.有的不识别头部信息.直接文件名是一个请求名
0 请登录后投票
   发表时间:2010-05-11  
谢谢,兄弟的帖子,搞定下载的问题了。。。
0 请登录后投票
论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics