jsp实现图片的上传和下载

将图片上传
1.添加使用jar包, jspSmartUpload.jar,并导包;
2.创建jsp页面,注意必须是“post”方式提交,form标签中一定要添加enctype=”multipart/form-data”

<body>
    <form action="<%=request.getContextPath()%>/UpServlet" method="post" enctype="multipart/form-data">
        <p><input type="file" name="file1">p>
        <p><input type="file" name="file2">p>
        <p><input type="file" name="file3">p>
        <p><input type="file" name="file4">p>
        <p><input type="submit" value="上传">p>
    form>
body>

3.添加servlet

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1)创建SmartUpload对象,
        SmartUpload su=new SmartUpload();
        //以及初始化initialize(getServletConfig(), request, response);
        su.initialize(getServletConfig(), request, response);
        //2)设置上传限制(文件的大小,类型)
        su.setMaxFileSize(1024*1024*9);
        su.setAllowedFilesList("jpg,png,gif");
        //3)调用SmartUpload对象的 upload()
        try {
            su.upload();
        } catch (SmartUploadException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //4)设置上传路径
        String path=this.getServletContext().getRealPath("images");
        File file=new File(path);
        if(!file.exists()) {
            file.mkdir();//创建文件夹
        }
        //5)读取上传文件的个数,开始单个文件的主传 
        int count=su.getFiles().getCount();
        List urls=new ArrayList<>();
        for (int i = 0; i < count; i++) {
            /*5.1)读取本次上传的文件
            5.2)判断文件的大小,如果为0则越过
            5.3)改文件名
            5.4)调用文件对象的 saveAs()方法
            5.5)数据库处理*/
        com.jspsmart.upload.File file1=su.getFiles().getFile(i);
            if(file1.getSize()==0) {
                continue;
            }
        String filename=file1.getFileName();
        try {
            file1.saveAs(path+"/"+filename);
            urls.add("images/"+filename);//要添加相对路径
        } catch (SmartUploadException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }
        request.setAttribute("urls", urls);
        request.getRequestDispatcher("success.jsp").forward(request, response);
        //6)修改跳转路径 结束

        //7)处理异常,完成跳转
    }

跳转至success.jsp中显示

<body>
    <c:forEach items="${urls }" var="url">
        <img alt="" src="${url }"><br>
    c:forEach>
body>

关于下载
在WebContent中建个文件夹,里面存放要下载的内容
这里写图片描述
jsp页面

<body>
    <a href="<%=request.getContextPath()%>/DownServlet">下载a>
body>

servlet

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*创建SmartUpload对象,初始化
        获得文件路径
            su.setContentDisposition(null);
        调用下载方法downloadFile(下载的文件的路径);*/
        SmartUpload su=new SmartUpload();
        su.initialize(getServletConfig(), request, response);
        String path=this.getServletContext().getRealPath("down/[Java参考文档]JDK_API_1_6_zh_CN.CHM");
        su.setContentDisposition(null);//禁止在浏览器中打开
        try {
            su.downloadFile(path);
        } catch (SmartUploadException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

你可能感兴趣的:(javaEE)