我写了个下载程序,用Java写的,但是写完以后发现下载大文件的时候报错,内存溢出,能看看是哪的问题么?
我写了个下载程序,用Java写的,但是写完以后发现下载大文件的时候报错,内存溢出,能看看是哪的问题么?Exceptioninthread"http-bio-8080-ex...
我写了个下载程序,用Java写的,但是写完以后发现下载大文件的时候报错,内存溢出,能看看是哪的问题么?Exception in thread "http-bio-8080-exec-10" java.lang.OutOfMemoryError: Java heap space,要怎么优化一下才行呢?我把代码也粘到下面吧,麻烦大神们帮我看看是代码的问题么?
public class downloadServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String path = request.getParameter("filename");
path = new String(path.getBytes("ISO-8859-1"), "utf-8");
System.out.println("23:"+path);
download(path, request, response);
}
@SuppressWarnings("deprecation")
public HttpServletResponse download(String path,
HttpServletRequest request, HttpServletResponse response) {
try {
System.out.println(request.getRealPath("/") + "/"
+ "upload/" + path);
File file = new File(request.getRealPath("/") + "/"
+ "upload/" + path);
String filename = file.getName();
InputStream fis = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
response.reset();
response.addHeader("Content-Disposition", "attachment;filename="
+ new String(filename.getBytes("utf-8"), "ISO-8859-1"));
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response
.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return response;
}
} 展开
public class downloadServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String path = request.getParameter("filename");
path = new String(path.getBytes("ISO-8859-1"), "utf-8");
System.out.println("23:"+path);
download(path, request, response);
}
@SuppressWarnings("deprecation")
public HttpServletResponse download(String path,
HttpServletRequest request, HttpServletResponse response) {
try {
System.out.println(request.getRealPath("/") + "/"
+ "upload/" + path);
File file = new File(request.getRealPath("/") + "/"
+ "upload/" + path);
String filename = file.getName();
InputStream fis = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
response.reset();
response.addHeader("Content-Disposition", "attachment;filename="
+ new String(filename.getBytes("utf-8"), "ISO-8859-1"));
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response
.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return response;
}
} 展开
展开全部
不能一次读取完,大文件很容易内存溢出。参考下:
public static void download(String path, HttpServletResponse response) throws Exception {
try {
File file = new File(path);
if (file.exists()) {
String filename = file.getName();
InputStream fis = new BufferedInputStream(new FileInputStream( file));
response.reset();
response.setContentType("application/x-download");
response.addHeader("Content-Disposition","attachment;filename="+ new String(filename.getBytes(),"iso-8859-1"));
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
byte[] buffer = new byte[1024 * 1024 * 4];
int i = -1;
while ((i = fis.read(buffer)) != -1) {
toClient.write(buffer, 0, i);
}
fis.close();
toClient.flush();
toClient.close();
try {
response.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
PrintWriter out = response.getWriter();
out.print("<script>");
out.print("alert(\"not find the file\")");
out.print("</script>");
}
} catch (IOException ex) {
PrintWriter out = response.getWriter();
out.print("<script>");
out.print("alert(\"not find the file\")");
out.print("</script>");
}
}
更多追问追答
追问
我仔细看了看,好像没看出哪些代码可以避免内存溢出呀,能不能再帮忙说的明白一点呢?多谢你了
追答
你的代码里面一次读取的:
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
展开全部
主要是你的Buffer开的太大了,4*1024*1024 也就是4G了。这个缓存很有点大。其实使用1024或者4096就行了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你把这么大的文件都读到内存里,当然受不了了,你去网上搜搜看看怎么分块,或者内存分流读取把!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询