Created
September 5, 2018 09:44
-
-
Save smallsong/79398b136716a9175a86a8f162cb4456 to your computer and use it in GitHub Desktop.
springboot 下载文件
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment