我的工作得到了服務器和客戶端應用程序,他們的工作完美而發送的小文件,但是當我嘗試發送例如電影文件是700MB超過插座它給了我也不會在Java
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
我搜索了互聯網,發現了一些關於發送大文件的教程,但不能理解它們,但我認爲我的porblem是在寫文件。
這是服務器使用寫我的文件代碼:
output = new FileOutputStream(directory + "/" + fileName);
long size = clientData.readLong();
byte[] buffer = new byte[1024];
while (size > 0 && (bytesRead = clientData.read(buffer, 0, (int) Math.min(buffer.length, size))) != -1) {
output.write(buffer, 0, bytesRead);
size -= bytesRead;
}
output.close();
這裏是我的客戶用來發送文件的代碼:
byte[] fileLength = new byte[(int) file.length()];
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
dis.readFully(fileLength, 0, fileLength.length);
OutputStream os = socket.getOutputStream();
//Sending size of file.
DataOutputStream dos = new DataOutputStream(os);
dos.writeLong(fileLength.length);
dos.write(fileLength, 0, fileLength.length);
dos.flush();
socket.close();
你可以做的最好的事情就是像這樣運行你的程序http://stackoverflow.com/questions/542979/using-heapdumponoutofmemoryerror-parameter-for-heap-dump-for-jboss然後你可以使用jvisualvm來分析它例如。 – biziclop 2012-07-12 10:01:40
我認爲你的問題在於你試圖找到X MB的內容到Y MB內存中,其中X> Y.如果這是真的,那你怎麼寫這個文件並不重要。這是例外情況告訴你的。 – duffymo 2012-07-12 10:02:55
你想吃一口西瓜,所以你死了。嘗試讀取一小段文件並將其發送出去並重復該操作。 – 2012-07-12 10:21:36