码迷,mamicode.com
首页 > 其他好文 > 详细

1 servlet实现下载文件

时间:2015-05-05 08:56:52      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:servlet 下载


技术分享技术分享

技术分享技术分享


代码实现:


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;


import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;




public class CodeReturn extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


response.setContentType("text/html");
this.doPost(request, response);
}


public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


response.setContentType("application/jar");
ServletContext servletContext = getServletContext();
InputStream inputStream = servletContext.getResourceAsStream("/1.jar");
OutputStream outputStream = response.getOutputStream();

byte [] buff = new byte[1024];
int n = 0;
while((n = inputStream.read(buff)) != -1) {
outputStream.write(buff, 0, n);
}
outputStream.flush();
outputStream.close();
}




}

1 servlet实现下载文件

标签:servlet 下载

原文地址:http://blog.csdn.net/u010919133/article/details/45487141

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!