springboot实现文件下载

springboot实现文件下载

1.新建一个springboot项目
2.pox.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.tuanzi</groupId>
    <artifactId>springboot-file-download</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-file-download</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

3.controller代码

package com.tuanzi.controller;
import com.tuanzi.utils.CommonUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;



@RestController
public class DownloadController {
    @RequestMapping("/download")
    public String download(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        String fileName = "girl.jpg";
        if (fileName != null){
            String realPath = "/Volumes/我的磁盘/图片/";
            File file = new File(realPath,fileName);
             fileName = new String(file.getName().getBytes("utf-8"));
             String suffixNmae = fileName.substring(fileName.lastIndexOf("."));
             String name = CommonUtils.generateUUID().toString();
             fileName = name + suffixNmae;
            if (file.exists()){
                response.setContentType("application/force-download");
                response.addHeader("Content-Disposition","attachment;fileName="+fileName);
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try{
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while(i != -1){
                        os.write(buffer,0,i);
                        i = bis.read(buffer);
                    }
                    System.out.println("success");
                }catch (Exception e){
                    e.printStackTrace();
                }finally {
                    if (bis != null){
                        try{
                            bis.close();
                        }catch (IOException e){
                            e.printStackTrace();
                        }
                    }
                    if (fis != null){
                        try{
                            fis.close();
                        }catch (IOException e){
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        System.out.println(fileName);
        return fileName;

    }
}

4.UUID代码

package com.tuanzi.utils;


import java.util.UUID;

/**
 *工具类的封装uuid
 */
public class CommonUtils {
    /**
     *生成 uuid, 即用来标识一笔单,也用做 nonce_str
     * @return
     */
    public static String generateUUID(){
        String uuid = UUID.randomUUID().toString().replaceAll("-","").substring(0,32);
        return uuid;
    }
    
}

5.运行项目访问http://localhost:8080/download就能下载文件了

赶快自己动手试一下吧!!

猜你喜欢

转载自blog.csdn.net/weixin_42370891/article/details/87894438