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;

      }

 

posted @ 2010-10-08 18:10  quietwalk  阅读(580)  评论(1编辑  收藏  举报