如何通过http上传文件到网站? (黑莓)

时间:2010-05-07 12:37:01

标签: blackberry jde

我需要通过http或ftp将文件上传到blackberry jde的网站。

1 个答案:

答案 0 :(得分:2)

高级视图:从HttpConnection打开OutputStream并将数据写入该输出流。主要问题是要选择使用哪个网络连接(我建议查看this, unless you're on OS 5.0 which has a similar feature built in)。至于通过FTP上传会比较困难,因为不支持内置在BlackBerry API中的FTP而是我将不得不考虑使用SocketConnection并自己实现部分FTP。

以下是一些可以帮助您入门的代码:

HttpConnection httpConn = (HttpConnection) Connector.open("<URL>");
FileConnection fileConn = (FileConnection) Connector.open("file:///<path>");
InputStream in = fileConn.openInputStream();
OutputStream out = httpConn.openOutputStream();
byte[] buffer = new byte[100];
int bytesRead = 0;
while((in.read(buffer) = bytesRead) > 0)
{
   out.write(buffer, 0, bytesRead);
}

当然,你需要处理异常,关闭流,检查它是否已成功上传等等