java - 如何获取文件列表
问题描述
我需要每次列出一个投资组合时,它都会返回该特定投资组合中存在的所有图像无效的
Photo Class
@Entity
public class Foto {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String nomeArquivo;
@ManyToOne(cascade = CascadeType.MERGE)
private Perfil autonomo;
@Column(length = 5_000_000)
private byte[] fotoByte;
private String tipoArquivo;
}//Getters and Setters
自治服务
@Autowired
FotosRepository fotosRepository;
@Autowired
PerfisRepository perfisRepository;
public List<byte[]> portfolio(int id){
if (perfisRepository.existsById(id)) {
return fotosRepository.findAllByAutonomoId(id).stream().map(f-> f.getFotoByte()).collect(Collectors.toList());
}
else {
return null;
}
} //Getters and Setters
控制器
@GetMapping("/portfolio/fotos/{id}")
public ResponseEntity<List<byte[]>> getPortfolioAutonomo(@PathVariable int id) throws IOException {
List<byte[]> result = autonomoService.portfolio(id);
return ResponseEntity.status(200).body(result);
}
这就是我可以通过其 ID 获取 1 张照片的方式
@GetMapping("/portfolio/{id}")
public ResponseEntity getPortfolio(@PathVariable int id){
Optional<Foto> anexoOptional = fotosRepository.findById(id);
if (anexoOptional.isPresent()) {
Foto anexo = anexoOptional.get();
return ResponseEntity.status(200)
.header("content-type", anexo.getTipoArquivo())
.header("content-disposition", "filename=" + anexo.getNomeArquivo())
.body(anexo.getFotoByte());
} else {
return ResponseEntity.status(404).build();
}
}
解决方案
代替
return fotosRepository.findAllByAutonomoId(id).stream().map(f-> f.getFotoByte()).collect(Collectors.toList());
你能试试吗
return fotosRepository.findAllById(id).stream().map(f-> f.getFotoByte()).collect(Collectors.toList());
如果这仍然不起作用,最好使用 @Query 实现。
推荐阅读
- maven-release-plugin - 在 Maven 发布插件 3.0.0 阶段添加自定义执行
- json - 如何以指定格式将外部 json 文件中的数据保存到新的 json 文件中?
- r - 如何从 R 中的数据帧中提取数据帧的名称?
- ssl - 验证 ActiveDomain 是否具有由第三方机构签署的证书
- python - Discord - 使用 python 显示来自特定类别的频道名称
- r - 选择结果分析中的第一个日期
- rename - 如何通过 youtube-dl 重命名下载的 youtube 播放列表
- imgur - REACT 应用程序可以直接访问 imgur API 还是需要在服务器端完成?
- node.js - 模拟 Typeorm QueryBuilder
- javascript - 计算函数的总执行时间