• 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;

          }

     

  • 相关阅读:
    laravel passport 全局捕获 401 ,返回数据给前台
    服务器上每次执行git pull,都提示输入用户名和密码
    jQuery-File-Upload 使用,jQuery-File-Upload上传插件
    Java返回结果封装,SpringBoot返回结果封装
    Java Spring文件上传,Java文件上传,Java通用文件上传
    Mdb文件工具类,UCanAccess使用,Access数据库操作
    Access MDB文件解析查询,Access数据库解析工具类MdbUtils
    jquery easyui combogrid Uncaught TypeError:Cannot read property
    soap调用Jar包冲突,SOAPMessageContext
    js打开新窗口,js打开居中窗口,js打开自定义窗口
  • 原文地址:https://www.cnblogs.com/quietwalk/p/1846017.html
Copyright © 2020-2023  润新知