こんにちは、次のコードを使用して、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)