简体   繁体   中英

Assigning name to a org.springframework.core.io.Resource object

I am looking for a way to assign/set a filename to a org.springframework.core.io.Resource object. The object does not have a function available to do this. The getFileName method in my case returns a null .

I do not want to create a multipartfile from the Resource object since my Open API spec does not accept it.

I achieved it like this

//Retrieved the object from S3
S3Object s3Object = amazonS3client.getObject(s3Config.getBucket(), key);
S3ObjectInputStream objectInputStream = s3Object.getObjectContent();
String fileName = s3Object.getObjectMetadata().getUserMetaDataOf("fileName");

//create a ByteArrayResource from the input stream and assigned the fileName
ByteArrayResource fileAsResource = new ByteArrayResource(IOUtils.toByteArray(objectInputStream)) {
                    @Override
                    public String getFilename() {
                        return fileName;
                    }

                    @Override
                    public long contentLength() {
                        return s3Object.getObjectMetadata().getContentLength();
                    }
                };

像这样添加标头后获取 url 的标头缺少文件名将解决此问题

 s3Object.getObjectMetadata().setHeader("fileName","page-1-tables.csv");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM