springMVC的文件上传下载功能实现

各位小伙伴

今天咱们分享的就是

文件上传与下载

文件的上传 

单个文件的上传

文件上传默认是采用的 apache 的上传组件 commons-fileupload进行上传

引入相关的 jar 包 

commons-fileupload.jar 

common-io.jar

编写一个文件上传功能测试的链接

通过这个链接进入到 uploadController,转到上传文件的页面

springMVC的文件上传下载功能实现_第1张图片

利用表单的 post 方式提交 

注意:

表单默认提交的 content-type 类型为  enctype="application/x-www-form-urlencoded" 需要把 content-type 的类型改为 enctype = "multipart/form-data" 

springMVC的文件上传下载功能实现_第2张图片

在 springmvc.xml 配置文件里面注册上传文件解析器 CommonsMultipartResolver

springMVC的文件上传下载功能实现_第3张图片

编写上传文件的功能代码

springMVC的文件上传下载功能实现_第4张图片

@RequestMapping("/uploadFile")

public String uploadFile(MultipartFile uploadFile,HttpServletRequest request){

 //将我们的文件保存到项目中某个指定的文件加下面

 //得到项目的绝对路径

 String rootPath = request.getServletContext().getRealPath("upload");

 //将上传的图片写入指定的文件

 if(uploadFile!=null){

    //获取上传文件的名称

    String fileName = uploadFile.getOriginalFilename();

    //获取上传上传的后缀

    String suffix = fileName.substring(fileName.lastIndexOf("."));

    //为了保险起见,我们给上传的图片重新指定一个名称

    String tempFileName = UUID.randomUUID().toString()+suffix;

    File fileTemp = new File(rootPath);

    if(!fileTemp.exists()){

     fileTemp.mkdir();

    }   

    File file = new File(rootPath+"\\"+tempFileName);

    try {

    //讲上传的文件写入指定路径

    uploadFile.transferTo(file);

    } catch (IllegalStateException e) {

     e.printStackTrace();

    } catch (IOException e) {

     e.printStackTrace();

    }           

    request.setAttribute("uploadFilePath", "upload/"+tempFileName);

  }       

  return "sysmanage/test/uploadTest";

}

在页面中添加一个显示上传图片的功能,可以看到我们上传的图片

发布测试

多个文件的上传

前面的步骤和单个文件长传一样,先来编写一下页面.

springMVC的文件上传下载功能实现_第5张图片

多个文件的上传需要利用数组,并且在 handler 方法的参数里面 MultipartFile[] uploadFileBatch 在参数前面要加上 @RequestParam

循环 uploadFileBatch 数组,处理方式更单个文件处理方式一样

springMVC的文件上传下载功能实现_第6张图片

@RequestMapping("/uploadFileBatch")public String uploadFileBatch(@RequestParam MultipartFile[]

uploadFileBatch,HttpServletRequest request){

String rootPath = request.getServletContext().getRealPath("upload");  Map mapPth = new HashMap();  if(uploadFileBatch!=null&&uploadFileBatch.length>0){    for(MultipartFile uploadFile :uploadFileBatch)   //将上传的图片写入指定的文件   if(uploadFile!=null){     //获取上传文件的名称     String fileName = uploadFile.getOriginalFilename();     if(fileName.lastIndexOf(".")!=-1){      String suffix = fileName.substring(fileName.lastIndexOf("."));        //为了保险起见,我们给上传的图片重新指定一个名称      String tempFileName=UUID.randomUUID().toString()+suffix;        //获取上传上传的后缀      File fileTemp = new File(rootPath);      if(!fileTemp.exists()){        fileTemp.mkdir();      }

File file = new File(rootPath+"\\"+tempFileName);      try {        //讲上传的文件写入指定路径        uploadFile.transferTo(file);      } catch (IllegalStateException e) {        e.printStackTrace();      } catch (IOException e) {        e.printStackTrace();      }      mapPth.put(fileName, tempFileName);      }

}    request.setAttribute("uploadMap", mapPth);   }  return "sysmanage/test/uploadTest";

}

发布项目,效果演示:

springMVC的文件上传下载功能实现_第7张图片

文件的下载

上面说了,文件的上传接下来咱们来说一下文件是如何下载下来的,先在页面上添加一个链接,可以把文件的地址传到我们的 downloadfile

springMVC的文件上传下载功能实现_第8张图片

在 uploadcontroller 中编写文件下载的功能.

springMVC的文件上传下载功能实现_第9张图片

@RequestMapping("/downloadFile")public void downloadFile(String fileName,HttpServletRequest request,

HttpServletResponse response){

if(fileName!=null){    String realPath = request.getServletContext().getRealPath("upload/");    File file = new File(realPath,fileName);    OutputStream out = null;    if(file.exists()){      //设置强制下载不打开      response.setContentType("application/force-download");     //设置文件名      response.setHeader("Content-Disposition", "attachment;filename="+fileName);     try {       out = response.getOutputStream();       out.write(FileUtils.readFileToByteArray(file));       out.flush();     } catch (IOException e) {       // TODO Auto-generated catch block       e.printStackTrace();     }finally{       if(out!=null)

try {         out.close();       } catch (IOException e) {         // TODO Auto-generated catch block         e.printStackTrace();       }      }                            }             }           }

发布项目,效果演示:

springMVC的文件上传下载功能实现_第10张图片

那么我们的

文件上传下载功能

就实现了

快来试一试吧

你可能感兴趣的:(springMVC的文件上传下载功能实现)