jsp/servlet实现简单上传和下载

使用JSP/Servlet简单实现文件上传与下载

jsp上传页面代码:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
  <head> 
    <title>文件上传</title> 
     
    <meta http-equiv="pragma" content="no-cache"> 
    <meta http-equiv="cache-control" content="no-cache"> 
    <meta http-equiv="expires" content="0"> 
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    --> 
 
  </head> 
   
  <body> 
    <form action="${pageContext.request.contextPath}/servlet/UploadServlet" method="post" enctype="multipart/form-data"> 
        name:<input name="name"/><br/> 
        file1:<input type="file" name="f1"/><br/> 
         
        <input type="submit" value="上传"> 
    </form> 
  </body> 
</html> 

 

上传servlet:

public class UploadServlet extends HttpServlet { 
 
    public void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException { 
        request.setCharacterEncoding("UTF-8"); 
        response.setContentType("text/html;charset=UTF-8"); 
        PrintWriter out = response.getWriter(); 
        System.out.print(request.getRemoteAddr()); 
        boolean isMultipart = ServletFileUpload.isMultipartContent(request); 
        if(!isMultipart){ 
            throw new RuntimeException("请检查您的表单的enctype属性,确定是multipart/form-data"); 
        } 
        DiskFileItemFactory dfif = new DiskFileItemFactory(); 
        ServletFileUpload parser = new ServletFileUpload(dfif); 
         
        parser.setFileSizeMax(3*1024*1024);//设置单个文件上传的大小 
        parser.setSizeMax(6*1024*1024);//多文件上传时总大小限制 
         
        List<FileItem> items = null; 
        try { 
            items = parser.parseRequest(request); 
        }catch(FileUploadBase.FileSizeLimitExceededException e) { 
            out.write("上传文件超出了3M"); 
            return; 
        }catch(FileUploadBase.SizeLimitExceededException e){ 
            out.write("总文件超出了6M"); 
            return; 
        }catch (FileUploadException e) { 
            e.printStackTrace(); 
            throw new RuntimeException("解析上传内容失败,请重新试一下"); 
        } 
         
        //处理请求内容 
        if(items!=null){ 
            for(FileItem item:items){ 
                if(item.isFormField()){ processFormField(item); }else{ 
                    processUploadField(item); 
                } 
            } 
        } 
         
        out.write("上传成功!"); 
    } 
    private void processUploadField(FileItem item) { 
        try { 
            String fileName = item.getName(); 
             
             
            //用户没有选择上传文件时 
            if(fileName!=null&&!fileName.equals("")){ 
                fileName = UUID.randomUUID().toString()+"_"+FilenameUtils.getName(fileName); 
                 
                //扩展名 
                String extension = FilenameUtils.getExtension(fileName); 
                //MIME类型 
                String contentType = item.getContentType(); 
                 
                 
                 
                //分目录存储:日期解决 
    //          Date now = new Date(); 
    //          DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); 
    //           
    //          String childDirectory  = df.format(now); 
                 
                 
                //按照文件名的hashCode计算存储目录 
                String childDirectory = makeChildDirectory(getServletContext().getRealPath("/WEB-INF/files/"),fileName); 
                 
                String storeDirectoryPath = getServletContext().getRealPath("/WEB-INF/files/"+childDirectory); 
                File storeDirectory = new File(storeDirectoryPath); 
                if(!storeDirectory.exists()){ 
                    storeDirectory.mkdirs(); 
                } 
                System.out.println(fileName); 
                item.write(new File(storeDirectoryPath+File.separator+fileName));//删除临时文件 
                 
            } 
        } catch (Exception e) { 
            throw new RuntimeException("上传失败,请重试"); 
        } 
         
    } 
    //计算存放的子目录 
    private String makeChildDirectory(String realPath, String fileName) { 
        int hashCode = fileName.hashCode(); 
        int dir1 = hashCode&0xf;// 取1~4位 
        int dir2 = (hashCode&0xf0)>>4;//取5~8位 
         
        String directory = ""+dir1+File.separator+dir2; 
        File file = new File(realPath,directory); 
        if(!file.exists()) 
            file.mkdirs(); 
         
        return directory; 
    } 
    private void processFormField(FileItem item) { 
        String fieldName = item.getFieldName();//字段名 
        String fieldValue; 
        try { 
            fieldValue = item.getString("UTF-8"); 
        } catch (UnsupportedEncodingException e) { 
            throw new RuntimeException("不支持UTF-8编码"); 
        } 
        System.out.println(fieldName+"="+fieldValue); 
    } 
 
    public void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException { 
        doGet(request, response); 
    } 
 

 

显示所有文件的servlet:

public class ShowAllFilesServlet extends HttpServlet { 
 
    public void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException { 
        String storeDirectory = getServletContext().getRealPath("/WEB-INF/files"); 
        File root = new File(storeDirectory); 
         
        //用Map保存递归的文件名:key:UUID文件名   value:老文件名 
        Map<String, String> map = new HashMap<String, String>(); 
        treeWalk(root,map); 
         
        request.setAttribute("map", map); 
        request.getRequestDispatcher("/listFiles.jsp").forward(request, response); 
    } 
    //递归,把文件名放到Map中 
    private void treeWalk(File root, Map<String, String> map) { 
        if(root.isFile()){ 
            String fileName = root.getName();//文件名      
            String oldFileName = fileName.substring(fileName.indexOf("_")+1); 
            map.put(fileName, oldFileName); 
        }else{ 
            File fs[] = root.listFiles(); 
            for(File file:fs){ 
                treeWalk(file, map); 
            } 
        } 
         
    } 
 
    public void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException { 
        doGet(request, response); 
    } 
 

 

显示所有文件的jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
  <head> 
    <title>title</title> 
     
    <meta http-equiv="pragma" content="no-cache"> 
    <meta http-equiv="cache-control" content="no-cache"> 
    <meta http-equiv="expires" content="0"> 
    <!-- 
    <link rel="stylesheet" type="text/css" href="styles.css"> 
    --> 
 
  </head> 
   
  <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}  <a href="${url}">下载</a><br/> 
    </c:forEach> 
  </body> 
</html> 

 

下载文件的servlet:

public class DownloadServlet extends HttpServlet { 
 
    public void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException { 
        String uuidfilename = request.getParameter("filename");//get方式提交的 
        uuidfilename = new String(uuidfilename.getBytes("ISO-8859-1"),"UTF-8");//UUID的文件名 
         
        String storeDirectory = getServletContext().getRealPath("/WEB-INF/files"); 
        //得到存放的子目录 
        String childDirecotry = makeChildDirectory(storeDirectory, uuidfilename); 
         
        //构建输入流 
        InputStream in = new FileInputStream(storeDirectory+File.separator+childDirecotry+File.separator+uuidfilename); 
        //下载  String oldfilename = uuidfilename.substring(uuidfilename.indexOf("_")+1); 
        //通知客户端以下载的方式打开 
        response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(oldfilename, "UTF-8")); 
         
        OutputStream out = response.getOutputStream(); 
         
        int len = -1; 
        byte b[] = new byte[1024]; 
        while((len=in.read(b))!=-1){ 
            out.write(b,0,len); 
        } 
        in.close(); 
        out.close(); 
         
    } 
 
    public void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException { 
        doGet(request, response); 
    } 
    //计算存放的子目录 
    private String makeChildDirectory(String realPath, String fileName) { 
        int hashCode = fileName.hashCode(); 
        int dir1 = hashCode&0xf;// 取1~4位 
        int dir2 = (hashCode&0xf0)>>4;//取5~8位 
         
        String directory = ""+dir1+File.separator+dir2; 
        File file = new File(realPath,directory); 
        if(!file.exists()) 
            file.mkdirs(); 
         
        return directory; 
    } 

 

转载:http://www.cnblogs.com/ys-wuhan/p/5772426.html

posted @ 2019-03-12 16:14  听惊雷  阅读(299)  评论(0编辑  收藏  举报