亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

內置網頁提供大文件下載內存溢出

項目需求,PC通過IP地址訪問靜態網頁,網頁提供文件下載,文件過大,直接oom,求老師指導

public?class?ClienDownloadService?extends?Service?{
????private?ExecutorService?threadPool;
????private?ServerSocket?serverSocket;
????private?InetSocketAddress?address;
????private?boolean?isEnable;

????public?IBinder?onBind(Intent?intent)?{
????????return?null;
????}

????public?void?onCreate()?{
????????super.onCreate();
????????initPlatfrom();
????????startAsync();
????}

????public?void?initPlatfrom()?{
????????threadPool?=?Executors.newCachedThreadPool();
????}

????private?void?startAsync()?{
????????isEnable?=?true;
????????new?Thread(new?Runnable()?{

????????????public?void?run()?{
????????????????try?{
????????????????????address?=?new?InetSocketAddress(8098);
????????????????????serverSocket?=?new?ServerSocket();
????????????????????serverSocket.bind(address);
????????????????????while?(isEnable)?{
????????????????????????final?Socket?socket?=?serverSocket.accept();
????????????????????????threadPool.execute(new?Runnable()?{
????????????????????????????public?void?run()?{
????????????????????????????????try?{
????????????????????????????????????buileRemoteSocket(socket);
????????????????????????????????}?catch?(Exception?e)?{
????????????????????????????????????e.printStackTrace();
????????????????????????????????}
????????????????????????????}
????????????????????????});
????????????????????}
????????????????}?catch?(Exception?e)?{
????????????????????e.printStackTrace();
????????????????}
????????????}
????????}).start();
????}

????private?void?buileRemoteSocket(Socket?socket)?throws?Exception?{
????????InputStream?in?=?socket.getInputStream();
????????BufferedReader?reader?=?new?BufferedReader(new?InputStreamReader(in));
????????String?line?=?"";
????????while?((line?=?reader.readLine())?!=?null)?{
????????????if?(line.startsWith("GET"))?{
????????????????String?uri?=?line.split("?")[1];
????????????????response(socket,?uri);
????????????}
????????}
????}

????public?void?response(Socket?socket,?String?assetsPath)?throws?Exception?{
????????assetsPath?=?assetsPath.substring(1);
????????if?(assetsPath.equals(""))?{
????????????assetsPath?=?"index.html";
????????}
????????InputStream?in?=?getAssets().open(assetsPath);
????????ByteArrayOutputStream?bos?=?new?ByteArrayOutputStream();
????????byte[]?buffer?=?new?byte[8192];
????????int?len?=?0;
????????while?((len?=?in.read(buffer))?!=?-1)?{
????????????bos.write(buffer,?0,?len);
????????}
????????byte[]?raw?=?bos.toByteArray();

????????in.close();

????????OutputStream?nos?=?socket.getOutputStream();
????????PrintStream?printer?=?new?PrintStream(nos);
????????printer.println("HTTP/1.1?200?OK");
????????printer.println("Content-length:"?+?raw.length);
????????if?(assetsPath.endsWith(".html"))?{
????????????printer.println("Content-Type:text/html");
????????}?else?if?(assetsPath.endsWith(".js"))?{
????????????printer.println("Content-Type:text/js");
????????}?else?if?(assetsPath.endsWith(".css"))?{
????????????printer.println("Content-Type:text/css");
????????}?else?if?(assetsPath.endsWith(".jpg"))?{
????????????printer.println("Content-Type:text/jpg");
????????}?else?if?(assetsPath.endsWith(".png"))?{
????????????printer.println("Content-Type:text/png");
????????}
????????printer.println();
????????printer.write(raw);
????????printer.flush();
????????printer.close();
????}

????public?void?onDestroy()?{
????????super.onDestroy();
????????stopAsync();
????}

????public?void?stopAsync()?{
????????isEnable?=?false;
????????try?{
????????????serverSocket.close();
????????}?catch?(IOException?e)?{
????????????e.printStackTrace();
????????}
????}

}


正在回答

1 回答

解決了么

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

內置網頁提供大文件下載內存溢出

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號