SSM框架分页展示Hdfs文件列表上传文件,删除文件,下载文件前后端代码
时间: 2023-09-22 19:06:17 浏览: 181
SSM框架实现文件上传及其分页
4星 · 用户满意度95%
### 回答1:
由于Hdfs是一个分布式文件系统,它的操作需要使用Hadoop API来进行操作,而不是像普通的文件系统一样直接访问文件路径。因此,我们需要使用Hadoop API来实现对Hdfs文件的上传、删除、下载等操作。
前端代码:
```
<!-- 分页展示Hdfs文件列表 -->
<table id="fileTable">
<thead>
<tr>
<th>文件名</th>
<th>大小</th>
<th>修改日期</th>
<th>操作</th>
</tr>
</thead>
<tbody id="fileList"></tbody>
</table>
<div id="pagination"></div>
<!-- 上传Hdfs文件 -->
<form id="uploadForm">
<input type="file" name="file"/>
<button type="submit">上传</button>
</form>
<!-- 删除Hdfs文件 -->
<form id="deleteForm">
<input type="hidden" name="filePath"/>
<button type="submit">删除</button>
</form>
<!-- 下载Hdfs文件 -->
<form id="downloadForm">
<input type="hidden" name="filePath"/>
<button type="submit">下载</button>
</form>
```
后端代码:
```
// 分页展示Hdfs文件列表
@RequestMapping("/list")
@ResponseBody
public Map<String, Object> listFiles(
@RequestParam(defaultValue = "/") String path,
@RequestParam(defaultValue = "1") int pageNum,
@RequestParam(defaultValue = "10") int pageSize) {
Map<String, Object> result = new HashMap<>();
try {
// 初始化Hadoop配置
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(URI.create("hdfs://localhost:9000"), configuration);
// 获取Hdfs文件列表
FileStatus[] fileStatuses = fs.listStatus(new Path(path));
List<Map<String, Object>> fileList = new ArrayList<>();
for (FileStatus fileStatus : fileStatuses) {
Map<String, Object> fileInfo = new HashMap<>();
fileInfo.put("name", fileStatus.getPath().getName());
fileInfo.put("size", fileStatus.getLen());
fileInfo.put("modificationTime", fileStatus.getModificationTime());
fileInfo.put("isDirectory", fileStatus.isDirectory());
fileList.add(fileInfo);
}
// 对文件列表进行分页
PageHelper.startPage(pageNum, pageSize);
PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(fileList);
// 返回分页结果
result.put("code", 200);
result.put("msg", "success");
result.put("data", pageInfo);
} catch (IOException e) {
result.put("code", 500);
result.put("msg", e.getMessage());
result.put("data", null);
}
return result;
}
// 上传Hdfs文件
@RequestMapping("/upload")
@ResponseBody
public Map<String, Object> uploadFile(MultipartHttpServletRequest request) {
Map<String, Object> result = new HashMap<>();
try {
// 获取上传的文件
MultipartFile file = request.getFile("file");
String filename = file.getOriginalFilename();
InputStream inputStream = file.getInputStream();
// 初始化Hadoop配置
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(URI.create("hdfs://localhost:9000"), configuration);
// 上传文件到Hdfs
Path dstPath = new Path("/" + filename);
FSDataOutputStream outputStream = fs.create(dstPath);
IOUtils.copyBytes(inputStream, outputStream, configuration);
outputStream.close();
inputStream.close();
result.put("code", 200);
result.put("msg", "success");
result.put("data", null);
} catch (IOException e) {
result.put("code", 500);
result.put("msg", e.getMessage());
result.put("data", null);
}
return result;
}
// 删除Hdfs文件
@RequestMapping("/delete")
@ResponseBody
public Map<String, Object> deleteFile(@RequestParam String filePath) {
Map<String, Object> result = new HashMap<>();
try {
// 初始化Hadoop配置
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(URI.create("hdfs://localhost:9000"), configuration);
// 删除Hdfs文件
Path path = new Path(filePath);
boolean success = fs.delete(path, true);
if (success) {
result.put("code", 200);
result.put("msg", "success");
result.put("data", null);
} else {
result.put("code", 500);
result.put("msg", "fail");
result.put("data", null);
}
} catch (IOException e) {
result.put("code", 500);
result.put("msg", e.getMessage());
result.put("data", null);
}
return result;
}
// 下载Hdfs文件
@RequestMapping("/download")
public void downloadFile(@RequestParam String filePath, HttpServletResponse response) {
try {
// 初始化Hadoop配置
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(URI.create("hdfs://localhost:9000"), configuration);
// 获取Hdfs文件流
Path path = new Path(filePath);
FSDataInputStream inputStream = fs.open(path);
// 设置响应头
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + path.getName());
// 输出Hdfs文件流
IOUtils.copyBytes(inputStream, response.getOutputStream(), configuration);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
```
### 回答2:
SSM框架是指Spring+Spring MVC+MyBatis,它是一种常用的Java Web开发框架。下面结合SSM框架,给出分页展示HDFS文件列表、上传文件、删除文件和下载文件的前后端代码示例。
1. 后端代码示例:
(1)分页展示HDFS文件列表的后端代码:
```java
@Service
public class HdfsService {
@Autowired
private FileSystem fileSystem;
public List<String> getFileList(int pageSize, int currentPage) throws IOException {
List<String> fileList = new ArrayList<>();
RemoteIterator<LocatedFileStatus> it = fileSystem.listFiles(new Path("/"), true);
int startIndex = (currentPage - 1) * pageSize; // 起始索引
int endIndex = currentPage * pageSize; // 结束索引
int currentIndex = 0; // 当前索引
while (it.hasNext()) {
LocatedFileStatus file = it.next();
if (currentIndex >= startIndex && currentIndex < endIndex) {
fileList.add(file.getPath().toString());
}
currentIndex++;
if (currentIndex >= endIndex) {
break;
}
}
return fileList;
}
}
```
(2)上传文件的后端代码:
```java
@Controller
public class UploadController {
@Autowired
private FileSystem fileSystem;
@RequestMapping("/upload")
@ResponseBody
public String upload(@RequestParam("file") MultipartFile file) throws IOException {
String fileName = file.getOriginalFilename();
Path outputPath = new Path("/", fileName);
try (InputStream inputStream = file.getInputStream()) {
try (OutputStream outputStream = fileSystem.create(outputPath)) {
IOUtils.copyBytes(inputStream, outputStream, 4096, true);
return "文件上传成功";
}
}
}
}
```
(3)删除文件的后端代码:
```java
@Controller
public class DeleteController {
@Autowired
private FileSystem fileSystem;
@RequestMapping("/delete")
@ResponseBody
public String delete(@RequestParam("fileName") String fileName) throws IOException {
Path path = new Path("/", fileName);
boolean result = fileSystem.delete(path, false);
if (result) {
return "文件删除成功";
} else {
return "文件删除失败";
}
}
}
```
(4)下载文件的后端代码:
```java
@Controller
public class DownloadController {
@Autowired
private FileSystem fileSystem;
@RequestMapping("/download")
public ResponseEntity<InputStreamResource> download(@RequestParam("fileName") String fileName) throws IOException {
Path path = new Path("/", fileName);
FSDataInputStream inputStream = fileSystem.open(path);
InputStreamResource resource = new InputStreamResource(inputStream);
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileName);
return ResponseEntity.ok()
.headers(headers)
.contentLength(fileSystem.getFileStatus(path).getLen())
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.body(resource);
}
}
```
2. 前端代码示例:
(1)分页展示HDFS文件列表的前端代码:
```html
<script type="text/javascript">
function getFileList(pageSize, currentPage) {
$.ajax({
url: '/hdfs/getFileList',
type: 'GET',
data: {
pageSize: pageSize,
currentPage: currentPage
},
success: function (data) {
// 处理返回的文件列表数据
}
});
}
</script>
```
(2)上传文件的前端代码:
```html
<form id="uploadForm" enctype="multipart/form-data">
<input type="file" name="file" id="file">
<button type="button" onclick="uploadFile()">上传</button>
</form>
<script type="text/javascript">
function uploadFile() {
var formData = new FormData($('#uploadForm')[0]);
$.ajax({
url: '/upload',
type: 'POST',
data: formData,
processData: false,
contentType: false,
success: function (data) {
// 处理文件上传成功后的逻辑
}
});
}
</script>
```
(3)删除文件的前端代码:
```html
<button type="button" onclick="deleteFile('fileName')">删除</button>
<script type="text/javascript">
function deleteFile(fileName) {
$.ajax({
url: '/delete',
type: 'GET',
data: {
fileName: fileName
},
success: function (data) {
// 处理文件删除成功后的逻辑
}
});
}
</script>
```
(4)下载文件的前端代码:
```html
<a href="/download?fileName=fileName">下载</a>
```
以上给出了SSM框架下分页展示HDFS文件列表、上传文件、删除文件和下载文件的前后端代码示例。注意修改代码中的路径和参数名等根据实际需求进行适配。
### 回答3:
以SSM框架为例,展示Hdfs文件列表并实现文件上传、删除和下载功能的前后端代码如下。
前端代码(使用HTML、CSS和JavaScript):
1. 展示Hdfs文件列表:
```html
<table>
<thead>
<tr>
<th>文件名</th>
<th>操作</th>
</tr>
</thead>
<tbody id="fileList">
<!-- Hdfs文件列表将动态加载到这里 -->
</tbody>
</table>
```
2. 文件上传:
```html
<input type="file" id="fileInput">
<button onclick="uploadFile()">上传</button>
<script>
function uploadFile() {
var file = document.getElementById("fileInput").files[0];
var formData = new FormData();
formData.append("file", file);
// 使用Ajax将文件上传到后端
// ...
}
</script>
```
3. 文件删除:
```html
<td>
<a href="javascript:void(0);" onclick="deleteFile('文件名')">删除</a>
</td>
```
```javascript
function deleteFile(filename) {
// 使用Ajax发送请求删除文件
// ...
}
```
4. 文件下载:
```html
<td>
<a href="javascript:void(0);" onclick="downloadFile('文件名')">下载</a>
</td>
```
```javascript
function downloadFile(filename) {
// 使用Ajax请求后端下载文件
// ...
}
```
后端代码(使用Java和SpringMVC):
1. 展示Hdfs文件列表(自定义Controller):
```java
@Controller
@RequestMapping("/file")
public class FileController {
@Autowired
private HdfsService hdfsService;
@GetMapping("/list")
@ResponseBody
public List<String> getFileList() {
return hdfsService.getFileList();
}
}
```
2. 文件上传(自定义Controller):
```java
@Controller
@RequestMapping("/file")
public class FileController {
@Autowired
private HdfsService hdfsService;
@PostMapping("/upload")
@ResponseBody
public String uploadFile(@RequestParam("file") MultipartFile file) {
hdfsService.uploadFile(file);
return "success";
}
}
```
3. 文件删除(自定义Controller):
```java
@Controller
@RequestMapping("/file")
public class FileController {
@Autowired
private HdfsService hdfsService;
@DeleteMapping("/delete")
@ResponseBody
public String deleteFile(@RequestParam("filename") String filename) {
hdfsService.deleteFile(filename);
return "success";
}
}
```
4. 文件下载(自定义Controller):
```java
@Controller
@RequestMapping("/file")
public class FileController {
@Autowired
private HdfsService hdfsService;
@GetMapping("/download")
public void downloadFile(@RequestParam("filename") String filename, HttpServletResponse response) {
hdfsService.downloadFile(filename, response);
}
}
```
其中,HdfsService为自定义的服务类,用于操作HDFS文件系统,如获取文件列表、上传文件、删除文件和下载文件。具体实现逻辑不在此处展示。至于前端向后端请求数据以及后端响应前端请求的具体实现,通常可以使用Ajax进行异步交互,并根据接口定义进行数据传输和处理。
阅读全文