文件下载 上传

来源:互联网 发布:手机画电路图软件 编辑:程序博客网 时间:2024/05/17 23:45
文件下载其实是触发了一个事件,跳转到另一界面
// 接受前台发送的下载请求路径和文件名String url = request.getParameter("url");System.out.println(url);response.setContentType("application/x-download");// 设置为下载application/x-downloadString[] p = url.split("/");for(String s:p){System.out.println(s);}String filedisplay = url.split("/")[0];// 下载文件时显示的文件保存名称response.addHeader("Content-Disposition", "attachment;filename="+ filedisplay);url = request.getServletContext().getRealPath("/") + url;File f = new File(url);if (f.exists()) {ServletOutputStream out = response.getOutputStream();FileUtils.copyFile(f, out);out.close();}
在另一界面中(servlet),添加如上代码,即可获得下载功能
原创粉丝点击