您的位置:首页 > 编程语言 > Java开发

springboot 下载文件

2018-09-04 09:49 519 查看
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import Java.nio.file.Paths;

/**
* 下载图片
*
* @author zcqshine
*/
@RestController
@RequestMapping("download")
public class DownloadController {

private final ResourceLoader resourceLoader;

@Value("${upload.file.path}")
private String filePath;

@Autowired
public DownloadController(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}

@GetMapping(value = "/{filename:.+}")
public ResponseEntity<?> getFile(@PathVariable String filename) {
try {
String path = Paths.get(filePath, filename).toString();
Resource resource = resourceLoader.getResource("file:" + path);
return ResponseEntity.ok(resource);
} catch (Exception e) {
throw e;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息