英文:
How to download a file as a blob/byte in Java/Spring
问题
I have a Spring Boot app that allows a user to upload files (such as .doc or jpegs) and saves them to an H2 database. While I'm able to save and retrieve the files from the database, and save the data as a byte, I am not able to allow the user to download the file to their machine.
现有的在线解决方案,比如这个链接,要么没有考虑将Blob保存为字节,要么涉及到复杂的SQL查询,似乎不适合我的应用程序或控制器操作上下文。
从H2数据库中检索数据后,我该如何将Blob下载到用户的机器上?
我当前的控制器操作是从href触发的:
@GetMapping("/download")
public String downloadFile() {
List<Files> files = fileMapper.getFiles();
Files newFile = files.get(0);
byte[] downloadedFile = newFile.getFileData();
return "home";
}
我还有我的repo链接:
https://github.com/jwolfe890/superDuperDrive1
英文:
I have a Spring Boot app that allows a user to upload files (such as .doc or jpegs) and saves them to an H2 database. While I'm able to save and retrieve the files from the database, and save the data as a byte, I am not able to allow the user to download the file to their machine.
Existing solutions I have found online, such as this one
either do not account for the Blob being saved as a byte or involve complex sql queries that don't seem to fit into the context of my app or controller action.
How can I download the blob to the user's machine after retrieving it from the H2 database?
The current controller action I have is triggered from an href:
@GetMapping("/download")
public String downloadFile() {
List<Files> files = fileMapper.getFiles();
Files newFile = files.get(0);
byte[] downloadedFile = newFile.getFileData();
return "home";
}
I also have my repo here:
答案1
得分: 2
你可以从org.springframework.core.io.Resource
导入Resource
,然后从你的文件创建InputStreamResource
,应该是这样的:
public ResponseEntity<Resource> downloadFile() {
byte[] bytes = yourMethodToGetByteBlobFromDB(....);
InputStreamResource resource = new InputStreamResource(new ByteArrayInputStream(bytes));
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Disposition", String.format("attachment; filename=your_file_name"));
return ResponseEntity.ok()
.headers(headers)
.contentLength(bytes.length)
.contentType("application/octet-stream")
.body(resource);
}
英文:
You can import Resource
from org.springframework.core.io.Resource
and create InputStreamResource
from your file, should looks like this:
public ResponseEntity<Resource> downloadFile() {
byte[] bytes = yourMethodToGetByteBlobFromDB(....);
InputStreamResource resource = new InputStreamResource(new ByteArrayInputStream(bytes));
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Disposition", String.format("attachment; filename=your_file_name"));
return ResponseEntity.ok()
.headers(headers)
.contentLength(bytes.length)
.contentType("application/octet-stream")
.body(resource);
}
答案2
得分: 2
以下代码对我来说完全正常运行。
在服务类中的下载逻辑
public ResponseEntity<Resource> downloadFile(String fileId) throws Exception
{
try
{
DBFile dbFile = dbFileStorageService.getFile(fileId);
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(dbFile.getFileType()))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + dbFile.getFileName() + "\"")
.body(new ByteArrayResource(dbFile.getData()));
}
catch(Exception e)
{
throw new Exception("Error downloading file");
}
}
DB文件存储服务类
public DBFile getFile(String fileId) throws Exception {
return dbFileRepository.findById(fileId)
.orElseThrow(() -> new Exception("File not found with id " + fileId));
}
DB文件存储库
@Repository
public interface DBFileRepository extends JpaRepository<DBFile, String> {
}
DB文件的实体类
@Entity
@Table(name = "files")
public class DBFile {
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
private String id;
private String fileName;
private String fileType;
@Lob
private byte[] data;
//getter and setter
}
英文:
Below code works perfectly for me.
Download logic in service class
public ResponseEntity<Resource> downloadFile(String fileId) throws Exception
{
try
{
DBFile dbFile = dbFileStorageService.getFile(fileId);
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(dbFile.getFileType()))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + dbFile.getFileName() + "\"")
.body(new ByteArrayResource(dbFile.getData()));
}
catch(Exception e)
{
throw new Exception("Error downloading file");
}
}
DB File Storeage service
public DBFile getFile(String fileId) throws Exception {
return dbFileRepository.findById(fileId)
.orElseThrow(() -> new Exception("File not found with id " + fileId));
}
DB File Repo
@Repository
public interface DBFileRepository extends JpaRepository<DBFile, String> {
}
Entity Class for DB file
@Entity
@Table(name = "files")
public class DBFile {
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
private String id;
private String fileName;
private String fileType;
@Lob
private byte[] data;
//getter and setter
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论