zoukankan      html  css  js  c++  java
  • Windows Mobile 使用GPRS上传及下载文件

     

    1、通过GPRS从服务器下载ZIP包并存储到设备上。

    (1)    服务器WS中将ZIP包转换成 byte[]并发送出去关键代码如下:

    //ZIP包转换成 byte[]并发送出去

        public byte[] convertZip2ByteArray()

        {

           byte[] buf = new byte[1024];

           int bufLength = -1;

           ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);

           BufferedInputStream bis = new BufferedInputStream(this.getClass()

                  .getResourceAsStream("HelloWordImpl.zip"));

           try {

               bufLength = bis.read(buf, 0, 1024);

               while (bufLength != -1) {

                  bos.write(buf, 0, bufLength);

                  bufLength = bis.read(buf, 0, 1024);

               }

               bis.close();

               bos.close();

               return bos.toByteArray();

           } catch (Exception e) {

               e.printStackTrace();

               return null;

           }

        }

    (2)     设备上进行存储的关键代码:

    //接收应用服务器发送的二进制流,并还原成文件,保存到设备文件系统中

            public static bool SaveFile(byte[] fileBinaryarray)//HelloWordImpl.zip

            {

                bool bReturnValue = false;

     

                if (fileBinaryarray.Length != 0)

                {

     

                    string strdFilePath;

                    try

                    {

                        strdFilePath = @"\存储卡\PDADataExchange\Receive\";// + "HelloWordImpl.zip";

     

                        if (!Directory.Exists(strdFilePath))

                        {

                            Directory.CreateDirectory(strdFilePath);

                        }

     

                        strdFilePath += "HelloWordImpl.zip";

     

                        FileStream objfilestream = new FileStream(strdFilePath, FileMode.Create, FileAccess.ReadWrite);

     

                        objfilestream.Write(fileBinaryarray, 0, fileBinaryarray.Length);

     

                        objfilestream.Close();

                        bReturnValue = true;

                    }

                    catch (Exception ex)

                    {

                        throw (ex);

                    }

                   

                }

                else

                {

                    MessageBox.Show("数据下载失败!");

                }

                return bReturnValue;

            }

    2、 使用GPRS将设备上传ZIP包到服务器,服务器接收并存储到文件系统中。

    (1)     设备将ZIP包转化成 byte 流并传送出去代码:

    public static byte[] SendFile(string strFileName)

            {

                string strdFilePath;

                strdFilePath = @"\存储卡\PDADataExchange\Send\" + strFileName;

     

                FileStream objfilestream = null;

                byte[] fileContents = null;

                try

                {

     

                    if (!File.Exists(strdFilePath))

                    {

                        MessageBox.Show("要发送的文件不存在,请查正!");

                    }

     

                    objfilestream = new FileStream(strdFilePath, FileMode.Open, FileAccess.Read);

     

                    int len = (int)objfilestream.Length;

     

                    fileContents = new byte[len];

     

                    objfilestream.Read(fileContents, 0, len);

                }

                catch (Exception ex)

                {

                    throw (ex);

                }

                finally

                {

     

                    objfilestream.Close();

                }

     

                return fileContents;

            }

    (2)    服务器接收 byte 流存储到文件系统代码:

    public boolean  convertByteArray2Zip(byte[] bts)

        {

       

           boolean bReturnValue=false;

          

           String fileName = "D:\\PDADataExchange\\Receive\\UnitAll.zip";

           try {

               BufferedOutputStream bos = new BufferedOutputStream(

                      new FileOutputStream(fileName));

               // 文件夹不存在报异常,文件不存在创建,存在覆盖          

              

               bos.write(bts, 0, bts.length);

               bos.close();

               bReturnValue=true;

           } catch (Exception e) {

               e.printStackTrace();

           }

          

           return bReturnValue;

          }

     

  • 相关阅读:
    WEBAPP开发技巧(手机网站开发注意事项)
    2014阿里前端线上笔试题
    [NOIp2008] 双栈排序 (二分图染色 + 贪心)
    [NOIp2012] 国王游戏(排序 + 贪心 + 高精度)
    关于错排公式以及扩展的一些小结论
    [NOI2009]诗人小G(dp + 决策单调性优化)
    Codeforces Round #429 (Div. 1) C. On the Bench(dp + 组合数)
    Educational Codeforces Round 33 (Rated for Div. 2) F. Subtree Minimum Query(主席树合并)
    Codeforces Round #511 (Div. 1) C. Region Separation(dp + 数论)
    Codeforces Round #471 (Div. 2) F. Heaps(dp)
  • 原文地址:https://www.cnblogs.com/quietwalk/p/1846017.html
Copyright © 2011-2022 走看看