我正在編寫客戶端服務器程序。客戶端位於同一WLAN上的另一臺計算機上。我面臨的問題是客戶端的這種例外情況:java.lang.OutOfMemoryError: Java heap space
。如何解決OutOfMemory:我的客戶端程序上的Java堆空間(使用Eclipse)
問題是,如果我也使用服務器計算機作爲客戶端(同一臺計算機上的Server-Client),它不會拋出此異常。但是,每次我從另一臺計算機運行客戶端以下載文檔時,都會引發異常。
請幫助一個簡單的方法來解決這個問題。我使用Eclipse。
下面是我的客戶端類:
public void run() {
try {
servsock = new ServerSocket(13330);
while (true) {
System.out.println("Waiting on question server...");
try {
sock = servsock.accept();
System.out.println("Accepted connection : " + sock);
// receive file
byte [] mybytearray = new byte [FILE_SIZE];
InputStream is = sock.getInputStream();
String LocalLocation = "C:\\NewEvalve\\LocalUploadedFiles\\"+clasx+"\\"+subject+"\\"+filename;
File LocalFile = new File(LocalLocation);
File file = new File("C:\\NewEvalve\\LocalUploadedFiles\\"+clasx+"\\"+subject);
if (!file.exists()) {
new File("C:\\NewEvalve\\LocalUploadedFiles\\"+clasx+"\\"+subject).mkdirs();
System.out.println(file+ " now exists.");
}
File outputfile = new File("C:\\NewEvalve\\LocalUploadedFiles\\"+clasx+"\\"+subject+"\\"+filename);
fos = new FileOutputStream(outputfile);
bos = new BufferedOutputStream(fos);
bytesRead = is.read(mybytearray,0,mybytearray.length);
current = bytesRead;
do {
bytesRead =
is.read(mybytearray, current, (mybytearray.length-current));
if(bytesRead >= 0) current += bytesRead;
} while(bytesRead > -1);
bos.write(mybytearray, 0 , current);
bos.flush();
System.out.println("File " + FILE_TO_RECEIVED
+ " downloaded (" + current + " bytes read)");
}
finally {
if (fos != null) fos.close();
if (bos != null) bos.close();
if (sock != null) sock.close();
} }
} catch (IOException e) {
e.printStackTrace();
}
......這裏是我的服務器類上傳文件:
public void uploadIt(InetAddress retur2, String fileLocation2){
try{
File myFile = new File (fileLocation2);
sock = new Socket(retur2, 13330);
System.out.println("Connection on "+sock.getRemoteSocketAddress());
os = sock.getOutputStream();
byte [] mybytearray = new byte [(int)myFile.length()];
fis = new FileInputStream(myFile);
bis = new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);
os.write(mybytearray,0,mybytearray.length);
fis.close();
bis.close();
os.flush();
os.close();
System.out.println("Sent " + fileLocation2 + "(" + mybytearray.length + " bytes)");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
if (bis != null)bis.close();
if (os != null) os.close();
if (sock!=null) sock.close();
System.out.println("Completed Upload");
} catch (IOException e) {
e.printStackTrace();
}}
}
你'FIS,二,操作系統,FOS,bos'收盤看起來並不健康。你首先應該送一些時間乾淨的一團糟。 – Serhiy 2014-10-10 12:10:05
你正在閱讀哪種類型的文件(二進制,文本等)? – OO7 2014-10-10 12:30:00
我正在閱讀短視頻,音頻和基於文本的文件 - 最多不能超過100MB(最差)。我需要關於如何以小塊讀取文件的幫助_任何人都可以請幫助一個示例代碼? – Jojo 2014-10-10 13:39:53