1. 程式人生 > >springboot簡易上傳下載

springboot簡易上傳下載

commons hashmap oid .get byte message ESS isp 讀取文件

1.導入上傳下載依賴:

     <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.2</version>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
    <!-- 添加thymeleaf -->
     <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-thymeleaf</artifactId>
     </dependency>

2.上傳:

1)前端頁面:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        function exproExcel() {
            
        }
    </script>
</head>
<body>
    <
h1>Spring Boot</h1> <a href="/Expro/excel">導出</a> <p th:text="${hello}"></p> <p>文件上傳</p> <form action="/upload/file" method="post" enctype="multipart/form-data"> 上傳:<input type="file" name="upfile"/> <button type="submit"
>提交</button> </form> </body> </html>

2)編寫跳到上傳頁面接口:

@Controller
public class HelloController {

    @RequestMapping("/hello")
    public String helloIndex(HashMap<String, Object> map){

        map.put("hello","Hello SpringBoot!");
        return "/index";
    }
}

3)編寫接收上傳文件接口:

@Controller
@RequestMapping("/upload")
public class UploadFileController {

    @RequestMapping(value = "/file")
    public @ResponseBody String uploadFile(@RequestParam("upfile") MultipartFile file, HttpServletRequest request){

        String message ="";

        try {

            if(!file.isEmpty()){

                FileOutputStream outputStream = new FileOutputStream("F:\\XIAOYAO"+"\\"+file.getOriginalFilename());

                outputStream.write(file.getBytes());
                outputStream.flush();
                outputStream.close();

                message="上傳成功!";


            }



        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            message="上傳失敗!";
        } catch (IOException e) {
            e.printStackTrace();
            message="上傳失敗!";
        }

            return message;
    }
}

3.下載:

1)編寫下載接口:

@RestController
@RequestMapping("/Expro")
public class ExprotExcelController {

    @RequestMapping("/excel")
    public void exproExcel(HttpServletRequest request, HttpServletResponse response) throws Exception{

        String path =  ClassLoader.getSystemResource("").toURI().getPath(); //獲取類加載地址

        System.out.println(path);

        File file = new File(path+"excelTempalte/模板.xlsx");
        FileInputStream fileInputStream = new FileInputStream(file); //讀取文件

        response.setHeader("Content-disposition", "attachment;filename=test.xlsx"); //設置響應頭和文件名字
        OutputStream outputStream = response.getOutputStream();

        //創建緩存區
        byte [] buffe = new byte[1024];

        int len =0;

        while ((len =fileInputStream.read(buffe))>0){
            outputStream.write(buffe,0,len);
        }
        

        fileInputStream.close();
        outputStream.flush();
        outputStream.close();
    }
}

springboot簡易上傳下載