写入文件时,代码抛出java.lang.IndexOutOfBoundsException

时间:2012-10-07 19:32:42

标签: java android apache-httpcomponents

您好我正在使用以下代码来处理android上的服务器端的文件上载。我已经用jsp测试了这个并且它按预期工作但是在android上它会引发异常java.lang.IndexOutOfBoundsException:参数超出范围在这条线上。 刚发现小于100 kb的文件正在成功上传。不知道原因是什么。当文件很大时,它也会随机抛出一个内存异常。

fileOut.write(dataBytes, startPos, (endPos - startPos));

有人可以建议为此解决这个问题。谢谢

相关代码:

        String contentType;
        byte[] dataBytes =null ;
        try {
            dataBytes = EntityUtils.toByteArray(entity);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        contentType=String.valueOf(request.getFirstHeader("Content-Type"));
        DataInputStream in=new DataInputStream(new ByteArrayInputStream(dataBytes));
        int formDataLength=dataBytes.length;
        int byteRead = 0;
        int totalBytesRead = 0;
        while (totalBytesRead < formDataLength)
        {
            try {
                byteRead = in.read(dataBytes, totalBytesRead,
                        formDataLength);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            totalBytesRead += byteRead;
        }
        String file = new String(dataBytes);
        String saveFile =file.substring(file.indexOf("filename=\"") + 10);
        saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
        saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,
                saveFile.indexOf("\""));
        int lastIndex = contentType.lastIndexOf("=");
        String boundary = contentType.substring(lastIndex + 1,
                contentType.length());
        int pos=0;
        pos = file.indexOf("filename=\"");
        pos = file.indexOf("\n", pos) + 1;
        pos = file.indexOf("\n", pos) + 1;
        pos = file.indexOf("\n", pos) + 1;

        int boundaryLocation = file.indexOf(boundary, pos) - 4;
        int startPos = ((file.substring(0, pos)).getBytes()).length;
        int endPos = ((file.substring(0,boundaryLocation)).getBytes()).length;

        FileOutputStream fileOut = new
                FileOutputStream("/sdcard/"+saveFile);

        fileOut.write(dataBytes, startPos, (endPos - startPos));//exception thrown here
        fileOut.flush();
        fileOut.close();
        JSONArray ja=new JSONArray();
        JSONObject jo=new JSONObject();
        try{
            jo.put("name", saveFile);
            jo.put("size", formDataLength);
            jo.put("url","ok"); 
            jo.put("thumbnail_url","");
            jo.put("delete_url", "");
            jo.put("delete_type","DELETE");
            ja.put(jo);
        }
        catch(JSONException je){
            je.printStackTrace();
        }
        StringEntity se=new StringEntity(ja.toString());
        response.setEntity(se);
        response.setHeader("Content-Type","application/json");
        response.setStatusCode(HttpStatus.SC_OK);

logcat index.out of bounds exception output:

10-07 21:39:11.197: E/AndroidRuntime(897): Uncaught handler: thread Thread-13 exiting due to uncaught exception
10-07 21:39:11.207: E/AndroidRuntime(897): java.lang.IndexOutOfBoundsException: Arguments out of bounds
10-07 21:39:11.207: E/AndroidRuntime(897):  at java.io.FileOutputStream.write(FileOutputStream.java:288)
10-07 21:39:11.207: E/AndroidRuntime(897):  at com.xlnc.webapp.FileListHandler.handle(FileListHandler.java:168)
10-07 21:39:11.207: E/AndroidRuntime(897):  at org.apache.http.protocol.HttpService.doService(HttpService.java:243)
10-07 21:39:11.207: E/AndroidRuntime(897):  at org.apache.http.protocol.HttpService.handleRequest(HttpService.java:187)
10-07 21:39:11.207: E/AndroidRuntime(897):  at com.xlnc.webapp.WebServer$WorkerThread.run(WebServer.java:140)

Logcat内存不足:

 10-07 21:35:36.587: E/AndroidRuntime(867): Uncaught handler: thread Thread-28 exiting due to uncaught exception
10-07 21:35:36.587: E/AndroidRuntime(867): java.lang.OutOfMemoryError
10-07 21:35:36.587: E/AndroidRuntime(867):  at java.nio.CharArrayBuffer.<init>(CharArrayBuffer.java:43)
10-07 21:35:36.587: E/AndroidRuntime(867):  at java.nio.ReadWriteCharArrayBuffer.<init>(ReadWriteCharArrayBuffer.java:47)
10-07 21:35:36.587: E/AndroidRuntime(867):  at java.nio.BufferFactory.newCharBuffer(BufferFactory.java:84)
10-07 21:35:36.587: E/AndroidRuntime(867):  at java.nio.CharBuffer.allocate(CharBuffer.java:57)
10-07 21:35:36.587: E/AndroidRuntime(867):  at java.nio.charset.CharsetDecoder.allocateMore(CharsetDecoder.java:293)
10-07 21:35:36.587: E/AndroidRuntime(867):  at java.nio.charset.CharsetDecoder.decode(CharsetDecoder.java:250)
10-07 21:35:36.587: E/AndroidRuntime(867):  at java.nio.charset.Charset.decode(Charset.java:768)
10-07 21:35:36.587: E/AndroidRuntime(867):  at java.lang.String.<init>(String.java:238)
10-07 21:35:36.587: E/AndroidRuntime(867):  at java.lang.String.<init>(String.java:193)
10-07 21:35:36.587: E/AndroidRuntime(867):  at com.xlnc.webapp.FileListHandler.handle(FileListHandler.java:141)
10-07 21:35:36.587: E/AndroidRuntime(867):  at org.apache.http.protocol.HttpService.doService(HttpService.java:243)
10-07 21:35:36.587: E/AndroidRuntime(867):  at org.apache.http.protocol.HttpService.handleRequest(HttpService.java:187)
10-07 21:35:36.587: E/AndroidRuntime(867):  at com.xlnc.webapp.WebServer$WorkerThread.run(WebServer.java:140)

0 个答案:

没有答案