一 前言
本文實現的文件下載是使用Apache 的 commons-fileupload 實現;在之前的springboot系列文件中已經講述過如何實現多文件上傳;這篇文件實現的文件下載功能主要是能在瀏覽器在線預覽或者下載至本地;
二 pom依賴
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
三 文件下載示例
參數 path 表示相對於根路徑的相對路徑
參數userAgent 是為了兼容IE判斷,如果使用谷歌,火狐瀏覽器就可以省略這個參數;
參數 filename 表示你下載至本地的文件名;
參數 inline表示是否要在線瀏覽,true是,false否;
/**
* @Author lsc
* @Description <p> 文件下載</p>
* @Date 2019/11/20 11:54
*/
@RestController
@RequestMapping("file")
public class DownloadController {
// 下載文件的根路徑
private String downloadPath = "C:\\mydata\\generator";
@GetMapping("download")
public ResponseEntity<byte[]> downlaodFile(HttpServletRequest request, @RequestParam("path") String path
, @RequestHeader("user-agent") String userAgent, @RequestParam("filename") String filename
,@RequestParam(required = false,defaultValue = "false") boolean inline ) {
// 根路徑加上傳參數的路徑構成文件路徑地址
String realPath = downloadPath + path;
File file = new File(realPath);
// 構建響應
ResponseEntity.BodyBuilder bodyBuilder = ResponseEntity.ok();
bodyBuilder.contentLength(file.length());
// 二進制數據流
bodyBuilder.contentType(MediaType.APPLICATION_OCTET_STREAM);
// 文件名編碼
try {
String encodeFileName = URLEncoder.encode(filename, "UTF-8");
// IE
if (userAgent.indexOf("MSIE")>0){
bodyBuilder.header("Content-Disposition","attachment;filename="+encodeFileName);
}else {
// 其他瀏覽器
if (inline){
// 在瀏覽器中打開
URL url = new URL("file:///" + file);
bodyBuilder.header("Content-Type",url.openConnection().getContentType());
bodyBuilder.header("Content-Disposition","inline;filename*=UTF-8''"+encodeFileName);
}else {
// 直接下載
bodyBuilder.header("Content-Disposition","attachment;filename*=UTF-8''"+encodeFileName);
}
}
// 下載成功返回二進制流
return bodyBuilder.body(FileUtils.readFileToByteArray(file));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
// 下載失敗直接返回錯誤的請求
return (ResponseEntity<byte[]>) ResponseEntity.badRequest();
}
}
四 tomcat配置
主要是開發特殊字符斜桿,如果是在linux上開發,那就自定義路徑,這個步驟可以省略;
@Configuration
public class ServerConfig {
//Url路徑添加支持字符
@Bean
public ConfigurableServletWebServerFactory webServerFactory() {
//設置Tomcate 支持
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
@Override
public void customize(Connector connector) {
connector.setProperty("relaxedQueryChars", "\\");
}
});
return factory;
}
}
五 瀏覽器測試
生成結果
六 源碼
github : youku1327
本套教程
- springboot入門 (1)
- Springboot自定義banner(2)
- springboot配置文件解析(3)
- springboot集成mybatis(4)
- springboot集成jdbcTemplate(5)
- spingboot單元測試(6)
- springboot集成thymeleaf(7)
- springboot多文件上傳(8)
- springboot文件下載(9)
- Springboot自定義異常類(10)
- springboot多環境配置(11)
- springboot自動配置原理解析(12)
- springboot集成restTemplate接口調用(13)
- springboot集成任務調度(14)
- springboot跨域CORS處理(15)
- springboot開啟GIZP壓縮(16)
- springboot集成logback(17)
- springboot集成Swagger(18)
- springboot集成actuator后台監控(19)
- springboot集成mybatis+oracle+druid(20)
- springboot 集成springsession(21)
- springboot集成jwt(22)
- springboot集成admin后台監控(23)
- springboot集成redis基礎篇(24)
- springboot集成redis緩存篇(25)
- springboot使用AOP日志攔截(26)
- springboot集成Validation參數校驗(27)
- springboot集成mybatisPlus(28)
- springboot集成shiro(29)
- springboot實現接口等冪次校驗(30)
- springboot-集成WebSockets(31)
- restTemplate源碼解析(32)
- SpringBoot使用@Async異步調用與線程池(33)
- 待續