我编写了一个小型客户端 - 服务器程序,其中服务器在Android手机上运行,客户端程序在我的PC上运行。当我从服务器向客户端发送小文件时(例如小文本文件,doc文件等),它工作得非常好。但是当我尝试发送更大的文件(比如大小为2到3 MB的mp3)时,由于数组大小溢出,客户端程序会抛出错误。
这是我的PC上运行的客户端程序:
package fileClient;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;
public class myFileClient {
/**
* @param args
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
int filesize=1022386;
int bytesRead;
int currentTot = 0;
String servAdd="10.142.100.161";
Socket socket = null;
InetAddress serverIP=InetAddress.getByName(servAdd);
byte [] bytearray = new byte [filesize];
socket=new Socket(serverIP, 4444);
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream("//home//evinish//MANA.mp3");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(bytearray,0,bytearray.length);
currentTot = bytesRead;
do {
bytesRead =
is.read(bytearray, currentTot, (bytearray.length-currentTot));
if(bytesRead >= 0) currentTot += bytesRead;
} while(bytesRead > -1);
bos.write(bytearray, 0 , currentTot);
bos.flush();
bos.close();
socket.close();
}
}
控制台输出显示以下错误:
/usr/lib/jvm/java-6-openjdk-amd64/bin/java: line 3: [: too many arguments
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
at java.lang.System.arraycopy(Native Method)
at java.io.BufferedOutputStream.write(BufferedOutputStream.java:128)
at fileClient.myFileClient.main(myFileClient.java:38)
我尝试将filesize变量增加到100MB左右,但这也不起作用。有人可以告诉我这方面的工作吗?