0

嗨,我正在使用以下代码在 android 的服务器端处理文件上传。我已经用 jsp 对其进行了测试,它按预期工作,但在 android 上它抛出异常 java.lang.IndexOutOfBoundsException: Arguments out of bounds on this line . 刚刚发现小于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 异常输出:

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)
4

0 回答 0