Spring mvc 上传单文件 多文件 下载文件

单文件上传

fileUpload.jsp

<%@ page isELIgnored="false" contentType="text/html;charset=UTF-8" language="java" %>


    Title


请选择文件:

${uploadFileError}

 

 DriverController

@RequestMapping("/someupload")
    public String someUpload(MultipartFile attach, HttpServletRequest request){

        String path=request.getSession().getServletContext().getRealPath("statics"+ File.separator+"upload");//上传的位置

      

        if(attach!=null){

          
                    String oldFileName=attach.getOriginalFilename(); //原文件名
                    String prefix= FilenameUtils.getExtension(oldFileName);//原后缀名

                    int fileSize=500000;//允许上传的最大的文件大小
                    if(attach.getSize()>fileSize){ //上传文件大小不得超过500kb
                        request.setAttribute("uploadFileError","*上传文件大小不得超过"+fileSize+"KB");
                        return "someFileUpload";
                    }else if(prefix.equalsIgnoreCase("jpg")   //限制上传文件的类型
                            || prefix.equalsIgnoreCase("png")
                            || prefix.equalsIgnoreCase("jpeg")
                            || prefix.equalsIgnoreCase("pneg")
                    ){
                        String fileName=System.currentTimeMillis()+ RandomUtils.nextInt(1000000)+"_Personal.jpg";
                        File file=new File(path,fileName);
                        if(!file.exists()){
                            file.mkdirs(); //如果文件不存在则创建一个文件夹
                        }
                        try {
                            attach.transferTo(file);


                        } catch (IOException e) {
                            e.printStackTrace();
                            request.setAttribute("uploadFileError", "*上传失败");
                            return "someFileUpload";
                        }

                    }else{
                        request.setAttribute("uploadFileError","* 上传图片格式不正确!");
                        return "someFileUpload";
                    }


                }else{
                    request.setAttribute("uploadFileError","* 你没有上传任何文件!");
                    return "someFileUpload";
                }

            
            request.setAttribute("filename",fileName);
            return "someFileUpload";


        }else{
            request.setAttribute("uploadFileError","您没有上传任何文件!");
            return "someFileUpload";
        }

    }

 多文件上传

 someFileUpload.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


请选择第一个文件: 请选择第二个文件: 请选择第三个文件:

${uploadFileError}

下载此图片

 DriverController

 @RequestMapping("/someupload")
    public String someUpload(MultipartFile[] attachs, HttpServletRequest request){

        String path=request.getSession().getServletContext().getRealPath("statics"+ File.separator+"upload");//上传的位置

        Map imgs=new HashMap<>();

        if(attachs!=null &&attachs.length>0){

            for (MultipartFile attach : attachs){

                if(!attach.isEmpty()){
                    String oldFileName=attach.getOriginalFilename(); //原文件名
                    String prefix= FilenameUtils.getExtension(oldFileName);//原后缀名

                    int fileSize=500000;//允许上传的最大的文件大小
                    if(attach.getSize()>fileSize){ //上传文件大小不得超过500kb
                        request.setAttribute("uploadFileError","*上传文件大小不得超过"+fileSize+"KB");
                        return "someFileUpload";
                    }else if(prefix.equalsIgnoreCase("jpg")   //限制上传文件的类型
                            || prefix.equalsIgnoreCase("png")
                            || prefix.equalsIgnoreCase("jpeg")
                            || prefix.equalsIgnoreCase("pneg")
                    ){
                        String fileName=System.currentTimeMillis()+ RandomUtils.nextInt(1000000)+"_Personal.jpg";
                        File file=new File(path,fileName);
                        if(!file.exists()){
                            file.mkdirs(); //如果文件不存在则创建一个文件夹
                        }
                        try {
                            attach.transferTo(file);

                        } catch (IOException e) {
                            e.printStackTrace();
                            request.setAttribute("uploadFileError", "*上传失败");
                            return "someFileUpload";
                        }
//                        request.setAttribute("fileName",fileName);
                        imgs.put(oldFileName,fileName);

                    }else{
                        request.setAttribute("uploadFileError","* 上传图片格式不正确!");
                        return "someFileUpload";
                    }


                }else{
                    request.setAttribute("uploadFileError","* 你没有上传任何文件!");
                    return "someFileUpload";
                }

            }

            request.setAttribute("images",imgs);
            return "someFileUpload";

        }else{
            request.setAttribute("uploadFileError","您没有上传任何文件!");
            return "someFileUpload";
        }

    }

 

文件下载

前端依旧为上面someFileUpload.jsp

DriverController

    @RequestMapping(value = "/downFile/{fileName}",produces = {})
    public void downFile(@PathVariable String fileName,HttpServletRequest request, HttpServletResponse response){

        if(fileName!=null){
            String realPath=request.getContextPath()+"statics"+ File.separator+"upload/";
            File file=new File(realPath,fileName);
            OutputStream os=null;

            if(file.exists()){
                response.setContentType("application/force-download"); //设置强制下载不打开
                    response.setHeader("Content-Disposition","attachment;filename="+fileName); //设置文件名
                try {
                    os=response.getOutputStream();
                    os.write(FileUtils.readFileToByteArray(file));
                    os.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    if(os!=null){
                        try {
                            os.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }else{
                request.setAttribute("uploadFileError","下载的文件不存在!");
            }
        }
    }

 

你可能感兴趣的:(Spring mvc 上传单文件 多文件 下载文件)