文件下载

来源:互联网 发布:mysql 查询记录总数 编辑:程序博客网 时间:2024/06/06 03:58
文件下载
注:  此处下载的文件是上例文件上传封装好目录的文件


-------------------------------------------------------------------------------------------
// 显示所有上传的文件,封装到域对象中,交给JSP去显示
public class ShowAllFilesServlet extends HttpServlet {


        public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
                // 创建一个Map,用来封装文件名称  key:UUID文件名   value: 老文件名
                Map<String, String> map = new HashMap<String, String>();
                // 得到存储文件的根目录
                String storePath = getServletContext().getRealPath("/WEB-INF/files");
                // 递归遍历其中文件
                File file = new File(storePath);
                treeWalk(file,map);
                // 交给JSP去显示: 如何封装数据,用MAP封装,key:UUID文件名   value: 老文件名
                request.setAttribute("map", map);
                request.getRequestDispatcher("/listFiles.jsp").forward(request, response);
        }


        
        // 递归遍历WEB-INF/files中的文件,把文件名称放到map中
        private void treeWalk(File file, Map<String, String> map) {
                if(file.isFile()){
                        // 是文件
                        String uuidName = file.getName();   // 真实文件名:  UUID_a_a.txt
                        String oldName = uuidName.substring(uuidName.indexOf("_")+1);  // 老文件名称: a_a.txt
                        map.put(uuidName, oldName);
                }else{
                        // 是目录
                        File[] fs = file.listFiles();
                        for(File f : fs){
                                treeWalk(f,map);
                        }
                }
        }


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


}


-------------------------------------------------------------------------------------------
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 <body>
        <h1>本站有以下图片</h1>    
        <c:forEach items="${map}" var="me">
                <c:url value="/servlet/DownloadServlet" var="url">
                        <c:param name="filename" value="${me.key}"></c:param>
                </c:url>
                ${me.value} &nbsp;&nbsp; <a href="${url}">下载</a><br/>
        </c:forEach>
  </body>
-------------------------------------------------------------------------------------------
// 文件的下载
public class DownloadServlet extends HttpServlet {


        public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
                response.setContentType("text/html;charset=UTF-8");
                OutputStream out = response.getOutputStream();
                
                String filename = request.getParameter("filename");   // get请求方式
                filename = new String(filename.getBytes("ISO-8859-1"),"UTF-8"); // 中文编码
                // 截取老文件名
                String oldFileName = filename.split("_")[1];   // 从UUID_A.JPG得到 A.JPG 
                
                // 得到存储路径
                String storePath = getServletContext().getRealPath("/WEB-INF/files");
                // 得到文件的全部路径
                String filePath = makeStorePath(storePath,filename)+"\\"+filename;  // /WEB-INF/files/1/11/UUID_a_a.jpg
                // 判断文件是否存在
                File file = new File(filePath);
                if(!file.exists()){
                        out.write("对不起,您要下载的文件可能已经不存在了".getBytes("UTF-8"));
                        return;
                }


                // 下载
                InputStream in = new FileInputStream(file);
                // 通知客户端以下载的方式打开
                response.setHeader("Content-Disposition","attachment;filename="+URLEncoder.encode(oldFileName,"UTF-8"));             
                byte[] b = new byte[1024];
                int len = 0;
                while((len=in.read(b))!=-1){
                        out.write(b,0,len);
                }
                in.close();
                out.write("下载成功".getBytes("UTF-8"));
        }


        // 得到文件的全部路径
        private String makeStorePath(String storePath, String filename) {
                int hashCode = filename.hashCode();
                int dir1 = hashCode & 0xf;   // 0000~1111 整数0~15 共16个
                int dir2 = (hashCode & 0xf0) >> 4 ;
                String path = storePath + "\\" + dir1 + "\\" + dir2;   // /WEB-INF/files/1/11
                File file = new File(path);
                if(!file.exists()){
                        file.mkdirs();
                }
                return path;
        }


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


}

-------------------------------------------------------------------------------------------
















0 0
原创粉丝点击