jsp实现图片的上传和下载

来源:互联网 发布:heyden 分层标定算法 编辑:程序博客网 时间:2024/05/17 09:17

将图片上传
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<String> 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();        }    }
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 去日本旅游怎么办签证 去美国旅游签证怎么办 我想去泰国怎么办签证 怎么办去泰国的签证 去韩国工作怎么办签证 去越南打工怎么办签证 越南到中国签证怎么办 去越南工作签证怎么办 马来西亚留学签证过期后怎么办 日本留学存款不够怎么办 没有工作单位怎么办签证 深户日本签证怎么办 土耳其跟团签证怎么办 公司取消交通车职工怎么办 出国健康证丢失怎么办 大三阳怎么办健康证 办不了健康证怎么办 办健康证不合格怎么办 美团健康证怎么办 便检取样很多怎么办 拉不出大便怎么办马上解决方法 无业人员怎么办健康证 健康证没身份证怎么办 身份证过期了怎么办护照 驾照体检表丢了怎么办 驾照体检表掉了怎么办 驾校体检表掉了怎么办 身份证掉了怎么办护照 驾照体检表过期了怎么办 助力车行驶证过期怎么办 c1证骑摩托车怎么办 别人知道驾驶证号码怎么办 摩托车卖了车牌怎么办 违章超过12分怎么办 违章扣分24分怎么办 驾驶证有效期过了怎么办 驾驶证到期没审怎么办 驾驶证扣不了分怎么办 集体户口怎么办户口本公证 强制保险单丢了怎么办 车子保险单丢了怎么办