分享

java

 PaladinL_WL 2018-01-30

各位小伙伴

今天咱们分享的就是

文件上传与下载


文件的上传 


单个文件的上传


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


引入相关的 jar 包 

commons-fileupload.jar 

common-io.jar


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



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



利用表单的 post 方式提交 

注意:

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



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



编写上传文件的功能代码



@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';
}


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



发布测试



多个文件的上传


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



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


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



@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';
}


发布项目,效果演示:




文件的下载


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



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



@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();
       }
      }                       
    }           
 }           
}


发布项目,效果演示:



那么我们的

文件上传下载功能

就实现了

快来试一试吧

java官方部落群:338412454,点击链接加入群【Java编程(8群)】:网页链接

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约