vue项目配合Javaweb实现用户增删改查文件的功能

javaweb文件操作参照   https://blog.csdn.net/shafatutu/article/details/51428066

根据项目需求修改部分代码

服务器硬盘中需要分别保存不同状态的项目申请书,所以需要创建文件夹

java servlet代码

        // 中文处理
        upload.setHeaderEncoding("UTF-8"); 


        // 构造临时路径来存储上传的文件
        // 这个路径相对当前应用的目录
        String uploadPath = getServletContext().getRealPath("") + File.separator + UPLOAD_DIRECTORY;
       
         
        // 如果目录不存在则创建
        File uploadDir = new File(getServletContext().getRealPath("") + File.separator+"checking_DIRECTORY");
        if (!uploadDir.exists()) {
            uploadDir.mkdir();
        }
        
        File uploadDir1 = new File(getServletContext().getRealPath("") + File.separator+"inProgress_DIRECTORY");
        if (!uploadDir1.exists()) {
            uploadDir1.mkdir();
        }   
        
        File uploadDir2 = new File(getServletContext().getRealPath("") + File.separator+"finished_DIRECTORY");
        if (!uploadDir2.exists()) {
            uploadDir2.mkdir();
        }   
        
        File uploadDir3 = new File(uploadPath);
        if (!uploadDir3.exists()) {
            uploadDir3.mkdir();

        }

然后再修改ListFileServlet.java
     String userName = request.getParameter("username"); 
    String stage = request.getParameter("stage"); 
        //获取上传文件的目录

        String uploadFilePath = this.getServletContext().getRealPath("//"+stage+"//"+userName);

       
        // 如果目录不存在则创建
        File uploadDir = new File(uploadFilePath);
        if (!uploadDir.exists()) {
            uploadDir.mkdir();
        }
        request.setAttribute("userName", userName);

        request.setAttribute("stage", stage);



扫描二维码关注公众号,回复: 1852184 查看本文章

传值给DownLoadServlet,DownLoadServlet代码添加接收

     String userName = request.getParameter("username");
    String stage = request.getParameter("stage");
        //得到要下载的文件名
        String fileName = request.getParameter("filename");  
        fileName = new String(fileName.getBytes("utf-8"),"UTF-8");
        //上传的文件都是保存在目录下的子目录当中

        String fileSaveRootPath=this.getServletContext().getRealPath("//"+stage+"//"+userName);


jsp  c:url部分绝对路径

c:url value="http://localhost:8080/MINDS/DownLoadServlet?stage=${stage}&username=${userName}"




前端部分添加发起请求,需要修改表单提交的数据和参数

   openSubmit(){
  this.showSubmit=true;
  document.getElementById('formSubmit').action="/api/MINDS/UploadServlet?name="+this.name;
  },
  funOpenMyproj1(){
  if(this.name!=''){
  this.showMyProj=true;
  document.getElementById('ifra').src='/api/MINDS/ListFileServlet?stage='+'checking_DIRECTORY'+'&username='+this.name;
 
  }
  },
  funOpenMyproj2(){
  if(this.name!=''){
  this.showMyProj=true;
  document.getElementById('ifra').src='/api/MINDS/ListFileServlet?stage='+'inProgress_DIRECTORY'+'&username='+this.name;
 
  }
  },
  funOpenMyproj3(){
  if(this.name!=''){
  this.showMyProj=true;
  document.getElementById('ifra').src='/api/MINDS/ListFileServlet?stage='+'finished_DIRECTORY'+'&username='+this.name;
 
  }
  },

猜你喜欢

转载自blog.csdn.net/qq_37828633/article/details/80830473