其他分享
首页 > 其他分享> > 测试文章

测试文章

作者:互联网

测试

你好,世界

@Controller
@RequestMapping("/cms")
public class CmsFileController extends BaseController implements CmsFileControllerApi {
    private GridFSService gridFSService;
    @Autowired
    public void setGridFSService(GridFSService gridFSService) {
        this.gridFSService = gridFSService;
    }

    @Override
    @GetMapping("/files/{fileId}")
    public ResponseEntity<byte[]> download(@PathVariable("fileId") String fileId) {
        GridFSFile gridFSFile = gridFSService.findGridFSFileById(fileId);
        if (gridFSFile == null) {
            ExceptionCast.cast(CmsCode.CMS_FILE_NOT_EXISTS);
        }

        try (InputStream inputStream = gridFSService.getInputStreamFromGridFS(gridFSFile)) {
            byte[] body = IOUtils.toByteArray(inputStream);

            HttpHeaders headers = new HttpHeaders();
            String fileName = URLEncoder.encode(gridFSFile.getFilename(), StandardCharsets.UTF_8.toString());
            headers.add("Content-Disposition", "attachment;filename=" + fileName);
            headers.add("Content-Length", String.valueOf(gridFSFile.getLength()));
            headers.add("Content-Type", "application/octet-stream");

            return new ResponseEntity<>(body, headers, HttpStatus.OK);

        } catch (IOException e) {
            ExceptionCast.cast(CmsCode.CMS_FILE_IO_ERROR);
        }

        return null;
    }
}

什么是事务?回答这个问题之前,我们先来看一个经典的场景:支付宝等交易平台的转账。假设小明需要用支付宝给小红转账 100000 元,此时,小明帐号会少 100000 元,而小红帐号会多 100000 元。如果在转账过程中系统崩溃了,小明帐号少 100000 元,而小红帐号金额不变,就会出大问题,因此这个时候我们就需要使用事务了。

标签:gridFSFile,帐号,headers,100000,测试,文章,gridFSService,fileId
来源: https://www.cnblogs.com/whileskies/p/12448761.html