学习springboot days3-postman实现上传以及下载文件

154 篇文章 15 订阅

一 postman概述

1.1postman的概述

1、接口调试工具安装和基本使用
2、下载地址:https://www.getpostman.com/

1.2 查询操作基本案例

1.3 上传excel文件

1.设置请求头

 2.设置请求文件格式

1.设置上传设置

 2.选择文件

3.测试

1.4 下载excel文件

1.设置请求连接,参数

2.选择要上传的文件 

3.下载excel内容

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
下面是Spring Boot集成HDFS和MySQL实现文件下载的示例代码: 1. 配置文件 application.properties: ``` #HDFS配置 hadoop.hdfs.uri=hdfs://localhost:9000 hadoop.hdfs.user.name=hadoop #MySQL配置 spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver #上文件存储路径 upload.path=/usr/local/uploads/ ``` 2. 实体类 FileEntity.java: ```java @Entity @Table(name = "file") @Data public class FileEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(nullable = false) private String name; @Column(nullable = false) private String path; @Column(nullable = false) private String type; @Column(nullable = false) private Long size; @Column(name = "create_time", nullable = false) private Date createTime; } ``` 3. HDFS工具类 HdfsUtils.java: ```java @Component public class HdfsUtils { @Value("${hadoop.hdfs.uri}") private String hdfsUri; @Value("${hadoop.hdfs.user.name}") private String hdfsUserName; private FileSystem fileSystem; @PostConstruct public void init() throws IOException { Configuration configuration = new Configuration(); configuration.set("fs.defaultFS", hdfsUri); fileSystem = FileSystem.get(configuration); } public void uploadFile(String srcPath, String destPath) throws IOException { Path src = new Path(srcPath); Path dst = new Path(destPath); fileSystem.copyFromLocalFile(src, dst); } public void downloadFile(String srcPath, String destPath) throws IOException { Path src = new Path(srcPath); Path dst = new Path(destPath); fileSystem.copyToLocalFile(src, dst); } public void deleteFile(String path) throws IOException { Path src = new Path(path); fileSystem.delete(src, true); } } ``` 4. 文件服务接口 FileService.java: ```java public interface FileService { FileEntity save(MultipartFile file) throws IOException; Resource loadFileAsResource(Long id) throws FileNotFoundException; void delete(Long id) throws IOException; Page<FileEntity> findByPage(int pageNum, int pageSize); } ``` 5. 文件服务实现类 FileServiceImpl.java: ```java @Service public class FileServiceImpl implements FileService { private final String uploadPath = System.getProperty("user.dir") + "/uploads/"; @Autowired private FileRepository fileRepository; @Autowired private HdfsUtils hdfsUtils; @Override public FileEntity save(MultipartFile file) throws IOException { String fileName = file.getOriginalFilename(); String fileType = fileName.substring(fileName.lastIndexOf(".") + 1); String filePath = uploadPath + fileName; File destFile = new File(filePath); file.transferTo(destFile); String hdfsFilePath = "/upload/" + fileName; hdfsUtils.uploadFile(filePath, hdfsFilePath); FileEntity fileEntity = new FileEntity(); fileEntity.setName(fileName); fileEntity.setPath(hdfsFilePath); fileEntity.setType(fileType); fileEntity.setSize(file.getSize()); fileEntity.setCreateTime(new Date()); return fileRepository.save(fileEntity); } @Override public Resource loadFileAsResource(Long id) throws FileNotFoundException { FileEntity fileEntity = fileRepository.findById(id).orElseThrow(() -> new FileNotFoundException("文件不存在")); String filePath = fileEntity.getPath(); String fileName = fileEntity.getName(); Path path = new Path(filePath); try { FileSystem fs = FileSystem.get(URI.create(hdfsUtils.getHdfsUri()), hdfsUtils.getConfiguration(), hdfsUtils.getHdfsUserName()); FSDataInputStream inputStream = fs.open(path); return new InputStreamResource(inputStream); } catch (IOException e) { e.printStackTrace(); throw new FileNotFoundException(e.getMessage()); } } @Override public void delete(Long id) throws IOException { FileEntity fileEntity = fileRepository.findById(id).orElseThrow(() -> new FileNotFoundException("文件不存在")); String filePath = fileEntity.getPath(); hdfsUtils.deleteFile(filePath); fileRepository.deleteById(id); } @Override public Page<FileEntity> findByPage(int pageNum, int pageSize) { Pageable pageable = PageRequest.of(pageNum, pageSize, Sort.Direction.DESC, "createTime"); return fileRepository.findAll(pageable); } } ``` 6. 文件控制器 FileController.java: ```java @RestController @RequestMapping("/file") public class FileController { @Autowired private FileService fileService; @PostMapping public ResponseEntity<?> uploadFile(@RequestParam("file") MultipartFile file) throws IOException { FileEntity fileEntity = fileService.save(file); URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}") .buildAndExpand(fileEntity.getId()).toUri(); return ResponseEntity.created(location).build(); } @GetMapping("/{id}") public ResponseEntity<Resource> downloadFile(@PathVariable Long id) throws FileNotFoundException { Resource resource = fileService.loadFileAsResource(id); return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"").body(resource); } @DeleteMapping("/{id}") public ResponseEntity<?> deleteFile(@PathVariable Long id) throws IOException { fileService.delete(id); return ResponseEntity.noContent().build(); } @GetMapping("/list") public ResponseEntity<Page<FileEntity>> getList(@RequestParam(defaultValue = "0") int pageNum, @RequestParam(defaultValue = "10") int pageSize) { Page<FileEntity> page = fileService.findByPage(pageNum, pageSize); return ResponseEntity.ok(page); } } ``` 7. 文件存储仓库 FileRepository.java: ```java public interface FileRepository extends JpaRepository<FileEntity, Long> { } ``` 这样,文件下载的功能就完成了。启动应用程序,可以使用POSTMAN或其他客户端上下载和删除文件了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值