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

发布于 2024-08-31 07:49:19 字数 45 浏览 3 评论 0原文

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

I need to upload a file through http or ftp to the website in blackberry jde.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最丧也最甜 2024-09-07 07:49:19

高级视图:您从 打开一个 OutputStream HttpConnection 并将数据写入该输出流。主要问题是选择使用哪个网络连接(我建议查看 这个,除非您使用的是内置类似功能的操作系统 5.0)。至于通过 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);
}

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

High level view: You open an OutputStream from an HttpConnection and write your data into that output stream. The main problem is going to be choosing which network connection to use (I recommend looking at this, unless you're on OS 5.0 which has a similar feature built in). As to uploading through FTP that will be somewhat more difficult as there is no support for FTP built into the BlackBerry API instead you'll have to look at using a SocketConnection and implementing part of FTP yourself.

Here's some code to get you started:

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);
}

Of course you'll need to deal with exceptions, close the streams, check that it was uploaded successfully, etc

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文