java-文件上传

1、准备上传下载的api组件

 

java-文件上传
<dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
</dependency> 
<dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3.1</version>
</dependency>
java-文件上传

 

2、编写前台上传表单

 

java-文件上传
<form action="index/newadd" method="post" enctype="multipart/form-data">
<table class="form">
      <tr>
           <td class="field">证件照:</td>
           <td><input type="file" id="wkpicpath" class="text" name="attachs" />  </td>
           <td class="field"id="hderrorinfo" style="margin-left:0px">${sessionScope.uploadwkError}</td>
     </tr>
     <tr>
          <td class="field">头像:</td>
          <td><input type="file" id="hdpicpath"  class="text" name="attachs" /></td>
          <td class="field"id="hderrorinfo" style="margin-left:0px">${sessionScope.uploadhdError}</td>
     </tr>
     <tr>
          <td></td>
          <td><label class="ui-blue"><input type="submit" name="submit" value="添加" /></label></td>
     </tr>
</table>
</form>
java-文件上传

 

3、编写controller层(多文件上传)

 

java-文件上传
 /*
     * 管理员新增用户
     */
    @RequestMapping(value="/newadd",method=RequestMethod.POST)
    public String doAddUser(HttpSession session,HttpServletRequest request,
                            @RequestParam(value="attachs",required=false)MultipartFile[] attachs,
                            User user){
        
        //定义两个上传文件的路径
        String wkpicpath = null;
        String hdpicpath = null;
        String errorinfo = null;
        //定义上传过程管理标记
        boolean flag = true;
        //定义文件保存的位置
        String path = request.getSession().getServletContext().getRealPath("statics"+File.separator+"uploadfiles");
        //循环读取文件信息
        for(int i=0;i<attachs.length;i++){
            MultipartFile attach = attachs[i];
            //判断文件是否为空
            if(!attach.isEmpty()){
                //判断是第几个文件
                if(i==0){
                    errorinfo = "uploadwkError";
                }else if(i==1){
                    errorinfo = "uploadhdError";
                }
            //获取源文件名
            String oldName= attach.getOriginalFilename();
            //获取源文件名后缀
            String prefixName = FilenameUtils.getExtension(oldName);
            
            int fileSize = 500000;
            //判断上传大小不得超过500K
            if(attach.getSize()>fileSize){
                session.setAttribute(errorinfo, "上传文件不得大于500k");
                flag = false;
            }else if(prefixName.equalsIgnoreCase("jpg")
                    || prefixName.equalsIgnoreCase("png")
                    || prefixName.equalsIgnoreCase("jpeg")
                    || prefixName.equalsIgnoreCase("pneg")){
                //判断上传格式
                //定义新的文件名,当前系统时间+随机数+固定后缀,
                //RandomUtils需要引入jar文件commons-lang.jar
//String fileName = System.currentTimeMillis()+RandomUtils.nextInt(1000000)+"personer.jpg";
                String fileName = System.currentTimeMillis()+"personer.jpg";
                //创建新的文件,用于接收用户上传的文件流
                File targetFile = new File(path, fileName);
                if(!targetFile.exists()){
                    targetFile.mkdirs();
                }
                
                //将上传的文件保存
                try {
                    attach.transferTo(targetFile);
                } catch (Exception e) {
                    e.printStackTrace();
                    session.setAttribute(errorinfo,"上传失败!");
                    flag = false;
                } 
                
                //更新上传的路径
                if(i==0){
                    wkpicpath = path + File.separator + fileName;
                }else if(i==1){
                    hdpicpath = path + File.separator + fileName;
                }                
            }else{
                session.setAttribute(errorinfo,"图片格式不正确!");
                flag = false;
            }                
           }        
        }        
        
        //准备User
        if(flag){
            user.setWkpicpath(wkpicpath);
            user.setHdpicpath(hdpicpath);
            user.setUserrole(1);
                        //插入数据库
            if(userService.findAddUser(user)>0){                
                return "redirect:/index/user";
            }            
        }        
        return "manager/user-add.jsp";
    }
java-文件上传

 原文地址:https://www.cnblogs.com/domi22/p/8059767.html

   
上一篇:ConcurrentHashMap中的2的n次方幂上舍入方法(转)


下一篇:java-日常练习-测试1w并发 ,用线程池和不用线程池内存和时间消耗对比