1.简单的实现
文件下载就是把数据放到response中给到前台
- public class Response_download extends HttpServlet {
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String path = this.getServletContext().getRealPath("/Download/定南中学.jpg");//获取文件的相对路径
- String filename = path.substring(path.lastIndexOf("\\")+1);//获取文件名称,在转化为子串
- //response.setHeader告诉浏览器以什么方式打开
- //假如文件名称是中文则要使用 URLEncoder.encode()编码
- //否则直接使用response.setHeader("content-disposition", "attachment;filename=" + filename);即可
- response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
- InputStream in = null ;
- OutputStream out = null ;
- try
- {
- in = new FileInputStream(path); //获取文件的流
- int len = 0;
- byte buf[] = new byte[1024];//缓存作用
- out = response.getOutputStream();//输出流
- while( (len = in.read(buf)) > 0 ) //切忌这后面不能加 分号 ”;“
- {
- out.write(buf, 0, len);//向客户端输出,实际是把数据存放在response中,然后web服务器再去response中读取
- } ……
-
-
- public class ListFileServlet extends HttpServlet {
2.具体实现//列出网站所有文件 public class ListFileServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String path = this.getServletContext().getRealPath("/WEB-INF/upload"); Map map = new HashMap(); listfile(new File(path), map); request.setAttribute("map", map); request.getRequestDispatcher("/listfile.jsp").forward(request, response); } // 如何保存迭归出来的资源 public void listfile(File file, Map map) { if (!file.isFile()) { File children[] = file.listFiles(); for (File f : children) { listfile(f, map); } } else { String filename = file.getName().substring( file.getName().indexOf("_") + 1); // 8347824284-343-343_a_b.txt map.put(file.getName(), filename); // <a href="/servlet?filename=文件在服务器的名称">文件的原始文件名</a> } } }
public class DownLoadServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 得到要下载的文件名 uuid String filename = request.getParameter("filename"); filename = new String(filename.getBytes("iso8859-1"), "UTF-8"); // 找出这个文件 url c:\\ String path = this.getServletContext().getRealPath("/WEB-INF/upload") + File.separator + getpath(filename); File file = new File(path + File.separator + filename); if (!file.exists()) { request.setAttribute("message", "对不起,您要下载的资源已被删除"); request.getRequestDispatcher("/message.jsp").forward(request,response); return; } // 得到文件的原始文件名 String oldname = file.getName().substring(file.getName().indexOf("_") + 1); // 通知浏览器以下载方式打开下面发送的数据 response.setHeader("content-disposition", "attachment;filename="+ URLEncoder.encode(oldname, "UTF-8")); FileInputStream in = new FileInputStream(file); int len = 0; byte buffer[] = new byte[1024]; OutputStream out = response.getOutputStream(); while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } in.close(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } public String getpath(String filename) { int hashcode = filename.hashCode(); // 121221 int dir1 = hashcode & 15; int dir2 = (hashcode >> 4) & 0xf; return dir1 + File.separator + dir2; // 3/5 } }