猪八戒APP
【周五前定标】java代码排错及修改
北京
其他工具软件开发
需求标题
【周五前定标】java代码排错及修改
需求描述
如下这个代码是建立一个socket长连接,然后不停的get文件到本地,但是运行的时候,发现如果get文件一次50-100个1M左右的文件,就会内存不停上涨。 不知道怎么优化一下?
=====
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
public class Main {
private static Socket socket;
private static InputStream is;
private static PrintWriter writer;
public static void main(String[] args) throws IOException{
socket=new Socket("tmp.518ccc.com",80);
is=socket.getInputStream();
writer=new PrintWriter(socket.getOutputStream());
get("/vmp/vnc.exe","vnc.exe");
get("/vmp/lcx.exe","lcx.exe");
get("/vmp/tm.mini.tar","tm.mini.tar");
writer.close();
is.close();
socket.close();
}
private static String nextLine() throws IOException{
StringBuilder sb=new StringBuilder();
while(true){
int b=is.read();
if(b==10){
break;
}
if(b!=13){
sb.append((char)b);
}
}
return sb.toString();
}
private static void get(String path, String filename) throws IOException {
System.out.println(path);
writer.println("GET "+path+" HTTP/1.1");
writer.println("Host: tmp.518ccc.com");
writer.println("Connection: keep-alive");
writer.println();
writer.flush();
int length=0;
String line=nextLine();
while(!"".equals(line)){
System.out.println(line);
if(line.startsWith("Content-Length:")){
length=Integer.parseInt(line.substring(16));
}
line=nextLine();
}
byte[] buf=new byte[1024];
int len;
System.out.println(length);
OutputStream fos=new FileOutputStream(filename);
try{
while(length>0&&(len=is.read(buf, 0, Math.min(length, 1024)))>0){
fos.write(buf, 0, len);
length-=len;
System.out.println(length);
}
}finally{
fos.close();
}
}
}
预算金额
¥300元
允许参与人才数量
--
人才所在地
--
人才类型
--
工作反馈要求
无要求
期望完成日期
--
开票类型要求
无要求
类似需求