j2me - 内容处置附件;文件名 - 如何获取它?
我正在尝试使用 j2me 应用程序下载 Rails 服务器提供的音频文件。 这是我的一段代码:
try {
connection = (HttpConnection) Connector.open("http://host.com/user/getaudio/65/");
connection.setRequestMethod(HttpConnection.GET);
connection.setRequestProperty("User-Agent", "Profile/MIDP-2.1 Configuration/CLDC-1.1");
System.out.println("HTTP Response: " + connection.getResponseMessage()); // OK
System.out.println("HTTP ResponseCode: " + connection.getResponseCode()); // 200
System.out.println("HTTP ResponseLength: " + connection.getLength()); // -1
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
HTTP 响应正常,代码:200。一切都很好,但 connection.getLength() 返回 -1。我意识到该文件是通过 Content-Disposition 标头附加的。 我知道如何通过 connection.getHeaderField() 到达它,但接下来该怎么办? 有可能以某种方式达到它吗?
问候, 彼得
I am trying to download an audio file served by Rails server with my j2me application.
Here is my piece of code:
try {
connection = (HttpConnection) Connector.open("http://host.com/user/getaudio/65/");
connection.setRequestMethod(HttpConnection.GET);
connection.setRequestProperty("User-Agent", "Profile/MIDP-2.1 Configuration/CLDC-1.1");
System.out.println("HTTP Response: " + connection.getResponseMessage()); // OK
System.out.println("HTTP ResponseCode: " + connection.getResponseCode()); // 200
System.out.println("HTTP ResponseLength: " + connection.getLength()); // -1
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
The HTTP response is OK, code: 200. Everything is fine but the connection.getLength() returns -1. I have realized that the file is attached via Content-Disposition header.
I know how to reach it with connection.getHeaderField() but what next?
Is it possible to reach it somehow?
Regards,
Peter
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
-1 可能意味着内容长度事先未知。只需从连接中读取数据,直到没有更多数据为止。
-1 probably means that the content length isn't known upfront. Just read from the connection until there's no more data.