多图上传到服务器

需要完成的效果图!




       导入jar包:commons-fileupload-1.2.2.jar     ;  commons-io-1.4.jar

       导入样式包:control   ; core

       写jsp页面:

<head>
<meta charset="UTF-8">
<title>由html5实现的文件上传预览功能--zyUpload</title>
<!-- 引用控制层插件样式 -->
<link rel="stylesheet" href="control/css/zyUpload.css" type="text/css">
<script src="http://www.lanrenzhijia.com/ajaxjs/jquery.min.js"></script>
<!-- 引用核心层插件 -->
<script src="core/zyFile.js"></script>
<!-- 引用控制层插件 -->
<script src="control/js/zyUpload.js"></script>
<!-- 引用初始化JS -->
<script src="core/lanrenzhijia.js"></script>
</head>
<body>
<h1 style="text-align:center;">zyUpload示例</h1>

<div id="demo" class="demo"></div>

</body>

       写servlet页面:

         String fileName= "";

//保存路径  

        String savePath = getServletContext().getRealPath("/images");  

        File saveDir = new File(savePath); 

// 如果目录不存在,就创建目录  

        if(!saveDir.exists()){  

            saveDir.mkdir();  

        }  

        // 创建文件上传核心类  

        DiskFileItemFactory factory = new DiskFileItemFactory();  

        ServletFileUpload sfu = new ServletFileUpload(factory);  

        //设置编码  

        sfu.setHeaderEncoding("UTF-8");  

        // 设置上传的单个文件的最大字节数为2M  

        sfu.setFileSizeMax(1024*1024*2);  

        //设置整个表单的最大字节数为10M  

        sfu.setSizeMax(1024*1024*10);  

        try{  

            // 处理表单请求  

            List<FileItem> itemList = sfu.parseRequest(request);  

            for (FileItem fileItem : itemList) {  

                // 对应表单中的控件的name  

                String fieldName = fileItem.getFieldName();  

                System.out.println("控件名称:" + fieldName);

                // 如果是普通表单控件  

                if(fileItem.isFormField()){  

                    String value = fileItem.getString();

                    //重新编码,解决乱码  

                    value = new String(value.getBytes("ISO-8859-1"),"UTF-8");  

                    System.out.println("普通内容:" + fieldName + "=" + value); 

                // 上传文件  

                }else{  

                    // 获得文件大小  

                    Long size = fileItem.getSize();  

                    // 获得文件名  

                    fileName = fileItem.getName();  

                    System.out.println("文件名:"+fileName+"\t大小:" + size + "byte");  

                    //设置不允许上传的文件格式  

                    if(fileName.endsWith(".exe")){  

                        request.setAttribute("msg", "不允许上传的类型!");  

                    }else{  

                        //将文件保存到指定的路径  

                     DateFormat dtf = new SimpleDateFormat("yyyyMMddHHmmss");

            String date = dtf.format(new Date());

            Random random = new Random();

            String rand = "";

            for(int j=0;j<3;j++){

                rand = rand+random.nextInt(10)+"";

            }

                       

                        File file = new File(savePath,rand+fileName);  

                        String path = "images/"+rand+fileName;

                        System.out.println(path);

                        //存数据库中

                        PhotoDao photoDao = new PhotoDao();

                        Photo photo = new Photo();

                        photo.setPhotoUrl(path);

                        HttpSession session = request.getSession();

                        int id =(Integer)session.getAttribute("PhotoSetId");

                        photo.setPhoto_SetId(id);

                        photoDao.savePhoto(photo);

                        

                        

                        fileItem.write(file);  

                        request.setAttribute("msg", "上传成功!");  

                    }  

                }  

            }  

        }catch(FileSizeLimitExceededException e){  

            request.setAttribute("msg", "文件太大");  

        }catch(FileUploadException e){  

            e.printStackTrace();  

        }catch(Exception e){  

            e.printStackTrace();  

        }

 改上传路径:(红色部分改成你servlet路径)

                  

这样我们就完成了!!!!微笑

 











  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

兔八哥的幸福生活

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值