2

我有一个文件“http://www.example.com/123.aes”的网址 (它包含超过 2MB 的 mp3 文件的数据)。现在我想从这个 url 获取数据。当我尝试使用 http 连接来获取数据时,它显示一个错误 -请求的实体太大。如何解决这个问题呢 ?。我的代码如下 -

 try {
    HttpConnection httpConn = (HttpConnection) Connector.open(httpURL, Connector.READ, true);
    final int iResponseCode = httpConn.getResponseCode();
    Dialog.alert(iResponseCode+"");
    InputStream is = httpConn.openInputStream();
    byte[] data = net.rim.device.api.io.IOUtilities.streamToBytes(is);
    String result = new String(data);
    Dialog.alert(result);

    FileConnection conn = (FileConnection)Connector.open("file:///store/home/user/pictures/"+System.currentTimeMillis()+".mp3", Connector.READ_WRITE);
    conn.create();
    OutputStream out = conn.openOutputStream();
    out.write(data);
    out.flush();
    out.close();
    conn.close(); 
   } catch (IOException e) {
    Dialog.alert(e+"");
    }

我也想将它保存为手机内存中的 mp3 文件。

4

2 回答 2

1

希望这会帮助你。尝试以下功能:

public static String getPage(String url) {    
    String response = "";
    try {
        HttpConnection conn = 
            (HttpConnection)Connector.open(url,Connector.READ_WRITE);
        InputStream input =conn.openInputStream(); 
        byte[] data = new byte[256];
        int len = 0;
        StringBuffer raw = new StringBuffer();
        while( -1 != (len = input.read(data))) {
            raw.append(new String(data, 0, len));
        }
        response = raw.toString();
        input.close();
    } 
    catch(Exception e) {
        Dialog.alert(e.toString());
    }
    return response;
}


对此函数的示例调用,

getPage("yourUrl;deviceside=true;interface=wifi");
于 2012-11-20T06:46:41.890 回答
1

终于我得到了答案——

ConnectionFactory connFact = new ConnectionFactory();
ConnectionDescriptor connDesc;
connDesc = connFact.getConnection(httpURL);
HttpConnection httpConn = (HttpConnection) connDesc.getConnection();
   try {
    httpConn.setRequestMethod(HttpConnection.GET);
    InputConnection inputConn = (InputConnection) httpConn;
    InputStream is = inputConn.openInputStream();
    byte[] data =IOUtilities.streamToBytes(is);
    Dialog.alert(original.toString());
    FileConnection conn = (FileConnection)Connector.open("file:///store/home/user/pictures/"+System.currentTimeMillis()+".mp3", Connector.READ_WRITE);
    conn.create();
    OutputStream out = conn.openOutputStream();
    out.write(original);
    out.flush();
    out.close();
    conn.close();  
   } catch (IOException e) {
      Dialog.alert(e+"");
   }
于 2012-11-20T10:55:57.837 回答