欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

上传下载

程序员文章站 2022-07-13 12:46:43
...
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 
 @RequestMapping(value = "/uploadfile",method = RequestMethod.POST)
   public int upload(@RequestParam("templateFile")MultipartFile file, HttpServletRequest  request,  User u ) {  
    String realPath = request.getSession().getServletContext().getRealPath("load");
     String date = sdf.format(new Date());
     System.out.println(realPath);
     File  folder=new File(realPath);
     if(!folder.isDirectory()) {
      folder.mkdirs();
     }
     String oldname = file.getOriginalFilename();  //用于数据库的filerealname
     Date filetime=null;
     try {
    filetime= sdf.parse(date);         //用于数据库的filetime
  } catch (ParseException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  }
     String newname=UUID.randomUUID().toString()+oldname.substring(oldname.lastIndexOf("."));  //上传于数据的filename
     
     try {
   file.transferTo(new File(folder,newname));
  } catch (IllegalStateException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
     
     Sys_school_level_materials s=new  Sys_school_level_materials();
     s.setFilerealname(oldname);
     s.setFiletime(filetime);
     s.setFilelocal(realPath);
     s.setUploadedby("管理员");
     s.setFilename(newname);

    return service.uploadinfor(s);
   }
//文件下载
 @RequestMapping("/Download")
 public void Download(HttpServletRequest request, HttpServletResponse response,Sys_school_level_materials down) {
    
  Sys_school_level_materials downloadInfo = service.DownloadInfo(down);
      String filelocal = downloadInfo.getFilelocal();//选择下载的路径名;
      String filename = downloadInfo.getFilename();//要下载的文件
   
      
      
  
 }
相关标签: java java