vue elementui + Java实现获取本地文件夹路径,绝对路径。(Ctrl C+V 即用)

2 篇文章 0 订阅

实现获取本地文件夹路径,不支持文件获取,文件上穿

最终获取后效果

选择文件夹得弹框效果

在这里插入图片描述

前端代码

<!DOCTYPE html>
<html>
<head>

<head>
    <!-- 兼容IE -->
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="../css/element-index.css">
    
    <!-- 自定义接口地址配置 -->
    <script src="../config/api.js"></script>
    <!-- import JavaScript -->
    <script type="text/javascript" src="../js/vue.js"></script>
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script type="text/javascript" src="../js/element-index.js"></script>
    <script type="text/javascript" src="../js/axios.min.js"></script>
   

</head>

<style>
    .el-table .warning-row {
        background: oldlace;
    }

    .el-table .success-row {
        background: #f4f8f2;
    }

    [v-cloak] {
        display: none;
    }
    .el-scrollbar__wrap{
        margin-right: 40px !important;
    	margin-left: 20px;
    }
</style>

<body>
<div id="app" v-cloak>
    <el-container>
        <el-header style="margin-top: 20px; height: 40px;">
            <el-form :inline="true"  size="mini" class="demo-form-inline">
                <el-form-item label="数据">
                    <el-input clearable v-model="dfpath" placeholder="数据" size="mini"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleGetPath('待测数据')" size="mini">...</el-button>
                </el-form-item>
                 <el-form-item label="数据">
                    <el-input clearable  v-model="jfpath"  placeholder="数据" size="mini"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleGetPath('检测数据') "size="mini">...</el-button>
                </el-form-item>
                <el-dialog title="" :append-to-body="true" :visible.sync="routeDialogVisible"  width="600px" :close-on-click-modal="false"  >
			      <el-form >
			        <el-form-item label="">
			          <el-input style="width:450px; padding-left:20px" size="mini" v-model="filePath">
			          </el-input>
			           <el-button style="float: right; margin: 5px 40px 0 0" size="mini" @click="backRoute()" >后退</el-button>
			        </el-form-item>
			        <el-scrollbar style="height: 350px">
			           <div v-for="(item, index) in fileList" v-if="index>=0" :key="index">
	                     <el-row :gutter="30">
	                        <el-col :span="20" style="display: flex;">
		                        <el-input  @click.native="clickData(fileList[index])" v-model="fileList[index]" style="margin-bottom: 10px;" type="button"></el-input>
	                        </el-col>
	                     </el-row>
				        </div>
			        </el-scrollbar>
			      </el-form>
			      <!-- 内容底部区域 -->
			      <span slot="footer" class="dialog-footer">
			        <el-button @click="closeGetPath()">取消</el-button>
			        <el-button type="primary" @click="confirmRoute()">确 定</el-button>
			      </span>
			   </el-dialog>
                
                
            </el-form>
        </el-header>
    </el-container>
</div>
</body>

<script>
    var data = {
        //文件夹数组
        fileList: [],
        routeDialogVisible:false,
        //文件夹路径
        filePath:'请选择文件所在路径',
        //选择的类别
        type:'',
        //待测数据
        dfpath:'',
        //检测报告
        jfpath:'',
    }
    //VUE内容实现
    var vue = new Vue({
        el: '#app',
        data: data,
        //初始加载
        created() {
          
        },
        methods: {
       	   //获取路径的方法
       	    handleGetPath(data) {
       		  this.type = data;
       		  console.log(data);
       		  var _this = this;
       		  //ajax调用方法
   		      axios.post(`${api.file}/fileList/`,{
   		    	files : _this.filePath
   		      })
              .then(function (res) {
                  if (res.data.code == 10010) {
                	  _this.fileList = res.data.dataList;
                  } else {
                      Vue.prototype.$message({
                          type: 'error',
                          message: res.data.msg
                      });
                  }
                })
                .catch(function (error) {
                    Vue.prototype.$message({
                        type: 'error',
                        message: "获取路径失败"
                    });
                });
       		  
       	      this.routeDialogVisible = true;
       	    },
       	    //关闭窗口
       	    closeGetPath() {
       	      this.fileList = {};
       	      this.filePath ='请选择文件所在路径';
       	      this.routeDialogVisible = false;
       	    },
       	    //确定按钮
       	    confirmRoute() {
       	    	var _this = this;
       	    	if(this.type == "待测数据"){
       	    		_this.dfpath = _this.filePath;
       	    	}else{
       	    		_this.jfpath = _this.filePath;
       	    	}
       	      this.fileList = {};
         	  this.filePath ='请选择文件所在路径';	
       	      this.routeDialogVisible = false;
       	    },
       	 //点击进入文件列表
       	    clickData(fileName) {
       	    	var _this = this;
       	    	if(this.filePath == '请选择文件所在路径'){
       	    		_this.filePath = fileName+"\\";
       	    	}else{
       	    		_this.filePath = this.filePath+"\\"+fileName;
       	    	}
       	   //ajax调用方法
     		      axios.post(`${api.file}/fileList/`,{
     		    	 files : _this.filePath
     		      })
                .then(function (res) {
                    if (res.data.code == 10010) {
                  	  _this.fileList = res.data.dataList;
                    } else {
                        Vue.prototype.$message({
                            type: 'error',
                            message: res.data.msg
                        });
                    }
                  })
                  .catch(function (error) {
                      Vue.prototype.$message({
                          type: 'error',
                          message: "获取路径失败"
                      });
                  });
       	    	
       	    },
       	    //向上一级
       	    backRoute() {
       	    	       	 	var _this = this;
	   	    	if(this.filePath == '请选择文件所在路径'){
	   	    	  Vue.prototype.$message({
                      type: 'error',
                      message: "未选择路径"
                  });
	   	    	}else{
	   	    		if(_this.filePath.length <=3){
	   	    			_this.filePath = '请选择文件所在路径';
	   	    		}else{
	   	    			_this.filePath = _this.filePath.substring(0,_this.filePath.lastIndexOf("\\"));	
	   	    		}
	   	    		
	   	    	}
	   	   //ajax调用方法
	 		      axios.post(`${api.file}/fileList/`,{
	 		    	 files : _this.filePath
	 		      })
	            .then(function (res) {
	                if (res.data.code == 10010) {
	              	  _this.fileList = res.data.dataList;
	                } else {
	                    Vue.prototype.$message({
	                        type: 'error',
	                        message: res.data.msg
	                    });
	                }
	              })
	              .catch(function (error) {
	                  Vue.prototype.$message({
	                      type: 'error',
	                      message: "获取路径失败"
	                  });
	              });
       	    },
        }
    })
</script>

</html>

这里只给出api.js 代码 其他需要的js 文件需要自己查找下载/或者下载附件获取

附件地址

api.js
//本地测试统一接口  如果该地址不可用,请在本地改为  http://ip:端口/项目名/
const host ='../';
//引用名称
const api = {
    'file': host + 'file',//获取文件目录
}


Java 代码

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.swing.filechooser.FileSystemView;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;

import com.ccit.java.utils.ResultRtn;
import com.ccit.java.utils.StatusCode;

@RequestMapping("/file")
@RestController
public class fileList {
	
	    @PostMapping("/fileList/")
	    public DeferredResult<ResultRtn> fileList(@RequestBody Map<String, String> map){
	    DeferredResult<ResultRtn> result = new DeferredResult<ResultRtn>();
	    List<String> fileName = new ArrayList<String>();
	    String files = map.get("files");
	    if(files.equals("请选择文件所在路径")) {
	    	//获取所有盘符
			FileSystemView fsv = FileSystemView.getFileSystemView();
			File[] fs = File.listRoots();
			for (int i = 0; i < fs.length; i++) {
				String name = fsv.getSystemDisplayName(fs[i]).toString();
				if(!name.isEmpty()) {
					int begin = name.lastIndexOf("(");
					int end = name.lastIndexOf(")");
					name = name.substring(begin+1, end);
					fileName.add(name);
					System.err.println(name);
				}
			}
			result.setResult(ResultRtn.of(StatusCode.SUCCESS.code, StatusCode.SUCCESS.msg, fileName));
	    }else {
	    	//获取指定目录下一级的所有文件夹
			String dirName= files;
			File file = new File(dirName);
			if(file.isDirectory()) {
				System.out.println("正在读取"+dirName+"目录....");
				String[] list = file.list();
				for(int i=0;i<list.length;i++) {
					File file2 = new File(dirName+"\\"+list[i]);
					if(file2.isDirectory()) {
						fileName.add(list[i]);
						System.out.println("文件夹:"+list[i]);
					}
				}
				result.setResult(ResultRtn.of(StatusCode.SUCCESS.code, StatusCode.SUCCESS.msg, fileName));
			}else {
				System.out.println(dirName+"不是一个目录。");
				  result.setResult(ResultRtn.of(StatusCode.NULL_RESULT.code, dirName+"不是一个目录。"));
			}
	    }
		return result;
	}

}


返回值实体类 自行解决一下

  • 5
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
实现消息通知,可以借助VueElementUI和Websockets,以下是基本的实现步骤: 1. 安装VueElementUI 使用命令行工具,进入项目目录,执行以下命令: ``` npm install vue npm install element-ui ``` 2. 引入ElementUI组件 在Vue的入口文件中,引入需要使用的ElementUI组件,例如Notification、MessageBox等。 ``` import Vue from 'vue' import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' Vue.use(ElementUI) ``` 3. 创建WebSocket连接 在Vue组件中通过Websockets连接后端服务器,建立WebSocket连接,接收服务器推送的消息。 ``` // 建立WebSocket连接 const webSocket = new WebSocket('ws://localhost:8080') // 监听WebSocket连接打开事件 webSocket.onopen = function () { console.log('WebSocket连接已打开') } // 监听WebSocket接收到消息事件 webSocket.onmessage = function (event) { console.log('WebSocket接收到消息:' + event.data) // 处理接收到的消息 } // 监听WebSocket连接关闭事件 webSocket.onclose = function () { console.log('WebSocket连接已关闭') } ``` 4. 处理接收到的消息 在WebSocket监听到有消息时,可以使用ElementUI的Notification组件弹出通知框,提醒用户有新消息。 ``` // 处理WebSocket接收到的消息 webSocket.onmessage = function (event) { console.log('WebSocket接收到消息:' + event.data) // 弹出通知框 const msg = JSON.parse(event.data) Notification({ title: '新消息', message: msg.content }) // 弹出确认框 MessageBox.confirm(msg.content, '新消息', { confirmButtonText: '查看详情', cancelButtonText: '忽略', type: 'info' }).then(() => { // 用户点击了查看详情按钮 // 处理跳转逻辑 }).catch(() => { // 用户点击了忽略按钮 // 不做任何处理 }) } ``` 以上就是使用VueElementUI和Websockets实现消息通知的基本步骤。需要注意的是,WebSocket连接的建立和关闭需要在组件的mounted和beforeDestroy生命周期函数中分别进行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Heart&Fire

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值