Windows Mobile 使用GPRS上传及下载文件
2010-10-08 18:10
274 查看
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;
}
(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;
}
相关文章推荐
- 利用SecureCRT上传、下载文件(使用sz与rz命令),超实用!
- SecureCRT上传下载文件教程(Zmodem使用教程)
- 利用SecureCRT上传、下载文件(使用sz与rz命令),超实用!【转】
- springmvc文件上传下载简单实现案例(ssm框架使用)
- Spring学习第三天——Spring MVC,使用注解方式,intellij的代码说明,ajax,文件的上传下载
- 使用SecureCRT上传和下载文件
- Mac使用技巧-Mac OSX 使用rz、sz 远程上传、下载文件
- SecureCRT使用Zmodem上传下载文件教程
- 利用SecureCRT上传、下载文件(使用sz与rz命令),超实用!
- android 使用AsyncHttpClient框架上传文件以及使用HttpURLConnection下载文件
- 利用SecureCRT上传、下载文件(使用sz与rz命令)
- 使用jspSmartUpload组件进行文件上传、下载
- iOS开发AFN使用二:AFN文件下载与文件上传
- 使用Struts2实现文件的上传与动态下载(1)
- 使用FTPClient文件上传下载遇到的问题
- C#使用七牛云存储上传下载文件、自定义回调
- android下使用FTP上传文件、下载文件、新建目录、删除文件工具类
- 使用FTP4J上传下载删除文件及文件夹
- XShell 4使用sftp上传下载文件命令
- 利用SecureCRT上传、下载文件(使用sz与rz命令),超实用!