上传文件到CDN,上传后文件错误。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这里提供一个基于SpringMVC和Hadoop API开发的展示HDFS文件列表、上传文件、删除文件、下载文件的简单示例代码。 1. 展示HDFS文件列表的后端代码 ```java package com.example.demo.controller; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; @Controller public class HDFSController { private final String HDFS_URL = "hdfs://localhost:9000"; @GetMapping("/list") public String listHDFS(Model model) throws URISyntaxException, IOException { Configuration configuration = new Configuration(); FileSystem fileSystem = FileSystem.get(new URI(HDFS_URL), configuration); Path path = new Path("/"); FileStatus[] status = fileSystem.listStatus(path); model.addAttribute("files", status); return "list"; } } ``` 2. 展示HDFS文件列表的前端代码 ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>HDFS文件列表</title> </head> <body> <h1>HDFS文件列表</h1> <table> <thead> <tr> <th>文件名</th> <th>文件大小</th> <th>修改时间</th> </tr> </thead> <tbody> <tr th:each="file : ${files}"> <td th:text="${file.getPath().getName()}"></td> <td th:text="${#numbers.formatDecimal(file.getLen()/1024/1024,1,'POINT',2)} + ' MB'"></td> <td th:text="${#dates.format(file.getModificationTime(),'yyyy-MM-dd HH:mm:ss')}"></td> </tr> </tbody> </table> </body> </html> ``` 3. 上传文件的后端代码 ```java package com.example.demo.controller; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; @Controller public class HDFSController { private final String HDFS_URL = "hdfs://localhost:9000"; @PostMapping("/upload") public String uploadHDFS(@RequestParam("file") MultipartFile file) throws URISyntaxException, IOException { Configuration configuration = new Configuration(); FileSystem fileSystem = FileSystem.get(new URI(HDFS_URL), configuration); String fileName = file.getOriginalFilename(); Path path = new Path("/" + fileName); fileSystem.copyFromLocalFile(new Path(file.getOriginalFilename()), path); return "redirect:/list"; } } ``` 4. 上传文件的前端代码 ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>上传文件</title> </head> <body> <h1>上传文件</h1> <form method="post" action="/upload" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" value="上传"> </form> </body> </html> ``` 5. 删除文件的后端代码 ```java package com.example.demo.controller; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; @Controller public class HDFSController { private final String HDFS_URL = "hdfs://localhost:9000"; @GetMapping("/delete/{fileName}") public String deleteHDFS(@PathVariable String fileName) throws URISyntaxException, IOException { Configuration configuration = new Configuration(); FileSystem fileSystem = FileSystem.get(new URI(HDFS_URL), configuration); Path path = new Path("/" + fileName); fileSystem.delete(path, false); return "redirect:/list"; } } ``` 6. 下载文件的后端代码 ```java package com.example.demo.controller; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.springframework.stereotype.Controller; import org.springframework.util.StreamUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.InputStream; import java.net.URI; import java.net.URISyntaxException; @Controller public class HDFSController { private final String HDFS_URL = "hdfs://localhost:9000"; @GetMapping("/download/{fileName}") public void downloadHDFS(@PathVariable String fileName, HttpServletResponse response) throws URISyntaxException, IOException { Configuration configuration = new Configuration(); FileSystem fileSystem = FileSystem.get(new URI(HDFS_URL), configuration); Path path = new Path("/" + fileName); InputStream inputStream = fileSystem.open(path); response.setHeader("Content-Disposition", "attachment; filename=" + fileName); StreamUtils.copy(inputStream, response.getOutputStream()); } } ``` 注意:需要在项目中引入hadoop-core和spring-web依赖。以上代码仅为示例,需要根据实际情况进行修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值