java interface list_列表与列表<?在java中扩展InterfaceI>

假设Foo和Bar是实现InterfaceI的两个类.

第二个(List)不允许向列表添加任何内容(null除外),因为列表包含的类型是未知的:它可以是List< Foo>或者列表< Bar>:你只是不知道.

因此,当您希望方法读取作为参数传递的列表元素时,通常将此表示法用于方法参数,并希望调用者能够使用List< InterfaceI>,List< Foo>调用您的方法.或列表< Bar>.使用List< InterfaceI>因为参数只接受List< InterfaceI>类型的列表.

让我们举一个具体的例子:你想要计算一个数字列表的最大双精度值.这种方法不需要在列表中添加或设置任何内容.它只是迭代元素,获取每个数字并计算它们的最大值.签名可以是

public double max(List list);

但是,你将无法做到

List ints = new ArrayList<>();

max(ints);

调用此方法的唯一方法是执行此操作

List ints = new ArrayList<>();

max(ints);

而如果您将方法声明为

public double max(List extends Number> list);

那么你可以做到

List ints = new ArrayList<>();

List longs = new ArrayList<>();

max(ints);

max(longs)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 后端代码: 1. HDFS文件列表展示 首先需要引入Hadoop的依赖,然后在代码连接HDFS,获取文件列表并返回给前端。 ```java @Controller @RequestMapping("/hdfs") public class HdfsController { private static final String HDFS_PATH = "hdfs://localhost:9000"; @RequestMapping("/list") @ResponseBody public List<String> list(@RequestParam(name = "path", defaultValue = "/") String path) throws Exception { Configuration configuration = new Configuration(); FileSystem fileSystem = FileSystem.get(new URI(HDFS_PATH), configuration); Path hdfsPath = new Path(path); RemoteIterator<LocatedFileStatus> iterator = fileSystem.listFiles(hdfsPath, false); List<String> fileList = new ArrayList<>(); while (iterator.hasNext()) { LocatedFileStatus fileStatus = iterator.next(); String filePath = fileStatus.getPath().toString(); fileList.add(filePath); } fileSystem.close(); return fileList; } } ``` 2. HDFS文件上传 在上传文件之前需要先判断目标路径是否存在,如果不存在则需要先创建目录。然后就可以通过输入流将本地文件上传到HDFS。 ```java @RequestMapping("/upload") @ResponseBody public String upload(@RequestParam("file") MultipartFile file, @RequestParam(name = "path", defaultValue = "/") String path) throws Exception { Configuration configuration = new Configuration(); FileSystem fileSystem = FileSystem.get(new URI(HDFS_PATH), configuration); Path hdfsPath = new Path(path); if (!fileSystem.exists(hdfsPath)) { fileSystem.mkdirs(hdfsPath); } String fileName = file.getOriginalFilename(); InputStream inputStream = file.getInputStream(); OutputStream outputStream = fileSystem.create(new Path(hdfsPath, fileName)); IOUtils.copy(inputStream, outputStream); fileSystem.close(); return "success"; } ``` 3. HDFS文件删除 在删除文件之前需要先判断目标路径是否存在,如果不存在则无法删除。然后就可以通过文件系统的delete方法将目标文件删除。 ```java @RequestMapping("/delete") @ResponseBody public String delete(@RequestParam(name = "path") String path) throws Exception { Configuration configuration = new Configuration(); FileSystem fileSystem = FileSystem.get(new URI(HDFS_PATH), configuration); Path hdfsPath = new Path(path); if (!fileSystem.exists(hdfsPath)) { return "file not exists"; } fileSystem.delete(hdfsPath, true); fileSystem.close(); return "success"; } ``` 4. HDFS文件下载 在下载文件之前需要先判断目标路径是否存在,如果不存在则无法下载。然后就可以通过文件系统的open方法获取文件的输入流,然后通过输出流将文件下载到本地。 ```java @RequestMapping("/download") public void download(@RequestParam(name = "path") String path, HttpServletResponse response) throws Exception { Configuration configuration = new Configuration(); FileSystem fileSystem = FileSystem.get(new URI(HDFS_PATH), configuration); Path hdfsPath = new Path(path); if (!fileSystem.exists(hdfsPath)) { throw new FileNotFoundException("file not found"); } FSDataInputStream inputStream = fileSystem.open(hdfsPath); response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=" + path.substring(path.lastIndexOf("/") + 1)); OutputStream outputStream = response.getOutputStream(); IOUtils.copy(inputStream, outputStream); fileSystem.close(); } ``` 前端代码: 1. HDFS文件列表展示 通过ajax请求获取后端接口返回的文件列表,然后将列表展示在页面上。 ```javascript $(document).ready(function () { loadFileList("/"); }); function loadFileList(path) { $.ajax({ url: "/hdfs/list", type: "get", dataType: "json", data: { "path": path }, success: function (data) { var fileList = $("#fileList"); fileList.empty(); for (var i = 0; i < data.length; i++) { var file = data[i]; var fileName = file.substring(file.lastIndexOf("/") + 1); var li = $("<li class='list-group-item'></li>"); if (file.indexOf(".") >= 0) { var icon = $("<i class='fa fa-file'></i>"); } else { var icon = $("<i class='fa fa-folder'></i>"); li.click(function () { var subPath = $(this).attr("data-path"); loadFileList(subPath); }); } li.append(icon); li.append(" " + fileName); li.attr("data-path", file); fileList.append(li); } }, error: function () { alert("load file list error"); } }); } ``` 2. HDFS文件上传 通过表单上传文件,然后通过ajax请求将文件上传到后端接口。 ```html <form id="uploadForm"> <div class="form-group"> <label for="file">Choose file</label> <input type="file" name="file" id="file"> </div> <div class="form-group"> <label for="path">Target directory</label> <input type="text" name="path" id="path" value="/"> </div> <button type="submit" class="btn btn-primary">Upload</button> </form> ``` ```javascript $(document).ready(function () { $("#uploadForm").submit(function (e) { e.preventDefault(); var formData = new FormData(this); $.ajax({ url: "/hdfs/upload", type: "post", data: formData, contentType: false, processData: false, success: function () { alert("upload success"); }, error: function () { alert("upload error"); } }); }); }); ``` 3. HDFS文件删除 通过点击按钮将目标路径发送到后端接口进行删除。 ```html <div class="form-group"> <label for="deletePath">Delete file</label> <input type="text" name="deletePath" id="deletePath"> <button class="btn btn-danger" onclick="deleteFile()">Delete</button> </div> ``` ```javascript function deleteFile() { var path = $("#deletePath").val(); $.ajax({ url: "/hdfs/delete", type: "post", data: { "path": path }, success: function (data) { if (data === "success") { alert("delete success"); } else { alert("delete error"); } }, error: function () { alert("delete error"); } }); } ``` 4. HDFS文件下载 通过点击文件名将目标路径发送到后端接口进行下载。 ```javascript $(document).ready(function () { $("body").on("click", "li", function () { var path = $(this).attr("data-path"); if (path.indexOf(".") >= 0) { window.location.href = "/hdfs/download?path=" + path; } }); }); ``` ### 回答2: SSM框架(Spring+Spring MVC+MyBatis)是一种常用的Java Web开发框架,它将Java的企业级开发框架整合在一起,提供了一套完整的解决方案。 下面以展示HDFS文件列表、上传文件、删除文件和下载文件为例,给出SSM框架的前后端代码实现: 前端代码: 展示HDFS文件列表: ```html <!--假设页面上有一个列表展示区域--> <ul id="fileList"> <!--动态生成HDFS文件列表--> </ul> <script> $(document).ready(function(){ //页面加载完成后,通过AJAX请求后端接口获取HDFS文件列表 $.ajax({ url: "后端接口地址", dataType: "json", type: "GET", success: function(data){ //将返回的文件列表数据动态插入到页面 for(var i=0; i<data.length; i++){ $("#fileList").append("<li>"+data[i]+"</li>"); } } }); }); </script> ``` 上传文件: ```html <form id="uploadForm"> <input type="file" id="fileInput" name="file" /> <button type="button" onclick="uploadFile()">上传</button> </form> <script> function uploadFile(){ var formData = new FormData(); formData.append("file", $("#fileInput")[0].files[0]); $.ajax({ url: "后端接口地址", data: formData, type: "POST", processData: false, contentType: false, success: function(data){ alert("上传成功"); } }); } </script> ``` 删除文件: ```html <button type="button" onclick="deleteFile('文件路径')">删除文件</button> <script> function deleteFile(filePath){ $.ajax({ url: "后端接口地址", data: {filePath: filePath}, type: "POST", success: function(data){ alert("删除成功"); } }); } </script> ``` 后端代码(Java): 展示HDFS文件列表: ```java @Controller public class FileController { @Autowired private HdfsService hdfsService; @RequestMapping("/file/list") @ResponseBody public List<String> getFileList(){ //调用HdfsService的方法获取HDFS文件列表 return hdfsService.getFileList(); } } ``` 上传文件: ```java @Controller public class FileController { @Autowired private HdfsService hdfsService; @RequestMapping(value = "/file/upload", method = RequestMethod.POST) @ResponseBody public String uploadFile(@RequestParam("file") MultipartFile file){ //调用HdfsService的方法上传文件到HDFS return hdfsService.uploadFile(file); } } ``` 删除文件: ```java @Controller public class FileController { @Autowired private HdfsService hdfsService; @RequestMapping(value = "/file/delete", method = RequestMethod.POST) @ResponseBody public String deleteFile(@RequestParam("filePath") String filePath){ //调用HdfsService的方法删除HDFS文件 return hdfsService.deleteFile(filePath); } } ``` 以上就是使用SSM框架展示HDFS文件列表、上传文件、删除文件和下载文件的前后端代码。当然,实际开发,还需要根据具体需求进行接口的具体实现和逻辑处理。 ### 回答3: SSM框架是指Spring + SpringMVC + MyBatis三个开源框架的整合使用。下面是一个展示HDFS文件列表、上传文件、删除文件以及下载文件的简单SSM框架的前后端代码实现。 1. 后端代码实现: (1)创建HdfsService接口,定义文件操作的方法。 ```java public interface HdfsService { List<String> getFileList(String path); void uploadFile(MultipartFile file, String path); void deleteFile(String path); void downloadFile(String path, HttpServletResponse response); } ``` (2)创建HdfsServiceImpl类,实现HdfsService接口,用于具体实现文件的操作。 ```java @Service public class HdfsServiceImpl implements HdfsService { @Autowired private FileSystem fileSystem; @Override public List<String> getFileList(String path) { try { FileStatus[] fileStatusArray = fileSystem.listStatus(new Path(path)); List<String> fileList = new ArrayList<>(); for (FileStatus fileStatus : fileStatusArray) { fileList.add(fileStatus.getPath().getName()); } return fileList; } catch (IOException e) { e.printStackTrace(); return null; } } @Override public void uploadFile(MultipartFile file, String path) { try { FSDataOutputStream outputStream = fileSystem.create(new Path(path + "/" + file.getOriginalFilename())); outputStream.write(file.getBytes()); outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } @Override public void deleteFile(String path) { try { fileSystem.delete(new Path(path), true); } catch (IOException e) { e.printStackTrace(); } } @Override public void downloadFile(String path, HttpServletResponse response) { try { FSDataInputStream inputStream = fileSystem.open(new Path(path)); IOUtils.copy(inputStream, response.getOutputStream()); response.flushBuffer(); } catch (IOException e) { e.printStackTrace(); } } } ``` 2. 前端代码实现: (1)创建Controller类,处理请求。 ```java @Controller @RequestMapping("/hdfs") public class HdfsController { @Autowired private HdfsService hdfsService; @RequestMapping("/list") public ModelAndView fileList(String path) { ModelAndView modelAndView = new ModelAndView("fileList"); List<String> fileList = hdfsService.getFileList(path); modelAndView.addObject("fileList", fileList); return modelAndView; } @RequestMapping("/upload") public String uploadFile(MultipartFile file, String path) { hdfsService.uploadFile(file, path); return "redirect:/hdfs/list?path=" + path; } @RequestMapping("/delete") public String deleteFile(String path) { hdfsService.deleteFile(path); return "redirect:/hdfs/list?path=" + new Path(path).getParent().toString(); } @RequestMapping("/download") public void downloadFile(String path, HttpServletResponse response) { hdfsService.downloadFile(path, response); } } ``` (2)创建fileList.jsp页面,在页面上展示文件列表。 ```html <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>文件列表</title> </head> <body> <h2>文件列表</h2> <% for (String file : (List<String>) request.getAttribute("fileList")) { %> <p><a href="/hdfs/download?path=<%= request.getParameter("path") + "/" + file %>"><%= file %></a> <a href="/hdfs/delete?path=<%= request.getParameter("path") + "/" + file %>">删除</a></p> <% } %> <form action="/hdfs/upload" method="post" enctype="multipart/form-data"> <input type="file" name="file"/> <input type="hidden" name="path" value="<%= request.getParameter("path") %>"/> <input type="submit" value="上传"/> </form> </body> </html> ``` 上面的代码是一个简单的SSM框架的示例,实现了展示HDFS文件列表、上传文件、删除文件和下载文件的功能。这些代码可以根据具体的项目需求进行修改和扩展

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值