文件下载

@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;
    }

}
五 浏览器测试
下载路径
http://localhost:8080/file/download?path=\2019\11\c7ed67a6-5502-479b-8934-736021426236.jpg&filename=5555.jpg

生成结果
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mcxiaochi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值