如何在Spring Boot项目中将文件从服务器下载到前端,创建Rest API用于文件下载但不能按预期运行?

问题描述 投票:1回答:1

一般来说,我已经创建了rest控制器,用于将文件从前端(React)上传和下载到我们的文件系统。正如预期的那样,上传效果很好。但是,下载rest api无法正常运行。它下载的文件只有12kb或smth。我的项目中缺少什么[configs]或者什么?请帮忙!任何评论或建议将提前感谢

@GetMapping("/get")
    fun getFile(@Valid data: FileDeleteDTO): ResponseEntity<Resource>{

        val header = HttpHeaders()

        val fileGetFromDb =  baseFileUploaderAttachmentService.getByUid(data.qquuid)
        if (!fileGetFromDb.isPresent)
            throw FileNotFoundException()
        val pathFileName = fileGetFromDb.get().filename + '.' + fileGetFromDb.get().extension
        val originalFileName = fileGetFromDb.get().originalName + '.' + fileGetFromDb.get().extension
//        val filePath = UPLOAD_ROOT_FOLDER + fileGetFromDb.get().path + pathFileName
        val filePath = UPLOAD_ROOT_FOLDER + fileGetFromDb.get().path
//        val file = File(filePath)
        header.contentType = (MediaType.valueOf(fileGetFromDb.get().mime_type!!))
        header.contentLength = fileGetFromDb.get().size
        header.set("Content-Disposition", "attachment; filename=$originalFileName")

        return ResponseEntity.ok()
                    .headers(header)
                    .body(loadFileAsResource(pathFileName,Paths.get(filePath)))

    }


    fun loadFileAsResource(fileName: String, fileStorageLocation: Path): Resource {

        try {
            val filePath = fileStorageLocation.resolve(fileName).normalize()
            val resource = UrlResource(filePath.toUri())
            return if (resource.exists()) {
                resource
            } else {
                throw FileNotFoundException("File not found $fileName")
            }
        } catch (ex: MalformedURLException) {
            throw Exception("File not found $fileName", ex)
        }

    }

This is the result from Insomnia which shows the result in which only some part of image is downloaded

spring-boot servlets kotlin download
1个回答
0
投票

我不知道发生了什么,但我通过添加一些如下所示的标题元素解决了上述问题:

@RequestMapping(value = ["/download"], method = [RequestMethod.GET])
@Throws(IOException::class)
fun downloadFile(@Valid data: FileDeleteDTO): ResponseEntity<Any> {

    val fileGetFromDb =  baseFileUploaderAttachmentService.getByUid(data.qquuid)
    val pathFileName = fileGetFromDb.get().filename + '.' + fileGetFromDb.get().extension
    val originalFileName = fileGetFromDb.get().originalName + '.' + fileGetFromDb.get().extension

    val filePath = UPLOAD_ROOT_FOLDER + fileGetFromDb.get().path + pathFileName

    val file = File(filePath)

    val resource = InputStreamResource(FileInputStream(file))
    val headers = HttpHeaders()

    headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"", originalFileName))
    headers.add("Cache-Control", "no-cache, no-store, must-revalidate")
    headers.add("Pragma", "no-cache")
    headers.add("Expires", "0")

    return ResponseEntity.ok().headers(headers).contentLength(
        file.length()
    ).contentType((MediaType.valueOf(fileGetFromDb.get().mime_type!!))).body(resource)
}

只是想知道Cache控件对这个问题有影响吗?我在哪里可以了解标题?

© www.soinside.com 2019 - 2024. All rights reserved.