jsp 文件安全下载
2018-07-20 来源:open-open
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class DownLoadServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String path = "C:/file"; String fileName = request.getParameter("filename"); File file = new File(path + "/" + fileName); //如果文件存在 if (file.exists()) { //设置响应类型及响应头 response.setContentType("application/x-msdownload"); response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); //读取文件 InputStream inputStream = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(inputStream); byte[] bytes = new byte[1024]; ServletOutputStream outStream = response.getOutputStream(); BufferedOutputStream bos = new BufferedOutputStream(outStream); int readLength = 0; while ((readLength = bis.read(bytes)) != -1) { bos.write(bytes, 0, readLength); } //释放资源 inputStream.close(); bis.close(); bos.flush(); outStream.close(); bos.close(); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
标签: isp
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
上一篇:UITextView 文本输入框
最新资讯
热门推荐