我正在开发一个 BB 应用程序,我需要在该应用程序中维护 HTTP 连接,并使用存储在服务器上的图像名称来获取写入该图像文档的文本。
我收到了 RTF 格式的回复。当我在打开的浏览器 Chrome 上直接点击服务器时,我的 RTF 文件被下载。现在我需要以编程方式执行该操作,
1) Either convert the bytes which are coming in response in a simple string format so that I can read that.
或者
2) Download the file as its happening on the browser manually so that by reading that file I read the information written in the document.
请建议我如何通过点击任何 URL 从服务器读取数据?
目前我正在使用此代码:
try {
byte []b = send("new_image.JPG");
String s = new String(b, "UTF-8");
System.out.println(s);
} catch (Exception e) {
e.printStackTrace();
}
public byte[] send(String Imagename) throws Exception
{
HttpConnection hc = null;
String imageName = "BasicExp_1345619462234.jpg";
InputStream is = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] res = null;
try
{
hc = (HttpConnection) Connector.open("http://webservice.tvdevphp.com/basisexpdemo/webservices/ocr.php?imgname="+imageName);
hc.setRequestProperty("Content-Type", "multipart/form-data;");
hc.setRequestMethod(HttpConnection.GET);
int ch;
StringBuffer sb= new StringBuffer();
is = hc.openInputStream();
while ((ch = is.read()) != -1)
{
bos.write(ch);
sb.append(ch);
}
System.out.println(sb.toString());
res = bos.toByteArray();
}
catch(Exception e){
e.printStackTrace();
}
finally
{
try
{
if(bos != null)
bos.close();
if(is != null)
is.close();
if(hc != null)
hc.close();
}
catch(Exception e2)
{
e2.printStackTrace();
}
}
return res;
}
响应如下:
{\rtf1\ansi\ansicpg1252\uc1\deflang1033\adeflang1033 ....................
我可以读取数据,但它没有格式化,所以我也可以通过编程方式读取它。