HTTP接続を維持する必要があるBBアプリに取り組んでおり、その画像ドキュメントに書かれたテキストを取得するためにサーバーに保存されている画像の名前を使用しています。
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...................
データを読み取ることはできますが、フォーマットされていないため、プログラムでも読み取ることができます。