SpringBoot文件上傳下載-創(chuàng)新互聯(lián)

第一部分 文件上傳

一、新建項(xiàng)目

二、配置maven

<?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.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>qiu</groupId>
    <artifactId>fileupload</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>fileupload</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-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </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>

三、SpringBoot配置文件application.properties

server.port=6555
# 上傳文件總的大值
spring.servlet.multipart.max-request-size=10MB
# 單個(gè)文件的大值
spring.servlet.multipart.max-file-size=10MB

## jsp
#spring.mvc.view.prefix=/WEB-INF/jsp/
#spring.mvc.view.suffix=.jsp

spring.mvc.static-path-pattern=/**

四、前端頁(yè)面

前端頁(yè)面放于resources目錄下面的public目錄中,springboot會(huì)默認(rèn)加載這個(gè)目錄。

十余年的灌云網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開(kāi)發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。成都全網(wǎng)營(yíng)銷(xiāo)推廣的優(yōu)勢(shì)是能夠根據(jù)用戶(hù)設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整灌云建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)從事“灌云網(wǎng)站設(shè)計(jì)”,“灌云網(wǎng)站推廣”以來(lái),每個(gè)客戶(hù)項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″>
        <title>文件上傳</title>
        <style>
            form{padding:20px 15px 15px 120px;}
            form{width:500px;height:300px;}
            form fieldset{border:0;align:center;}
            form label{display:inline-block;width:70px;text-align:right}
            input{width:240px;height:30px;}
            #submit{width:200px;height:35px;position:relative;left:70px;}
            #submit:hover{cursor: pointer;background:#D8D8D8;}
            body{width:100%;height:100%;background-image: url(img/1.jpg);}
            #box{position:fixed;left:0px;right:0px;width:706px;margin-left:auto;margin-right:auto;top:100px;}
            #code{width: 70px;height: 40px;font-size:18px;}
            #captcha_img{position:relative;bottom:-15px;}
            a{position:relative;bottom:-10px;}
        </style>
    </head>
    <body >
    <div id="box">
        <form method="post" action="upload" enctype="multipart/form-data" >
            <fieldset>
                <div >
                    文件上傳
                </div>
                <!-- <p>
                  <label>用戶(hù)名:</label>
                  <input type="text"name="username"placeholder="用戶(hù)名"required="required"/>
                </p>
                <p>
                  <label>密碼:</label>
                  <input type="password"name="password"placeholder="密碼"required="required"/>
                </p> -->

                <p>
                    <label>選擇文件:</label>
                    <input type="file" name="file">
                </p>
                <p>
                    <label>保存路徑:</label>
                    <input type="text" name="path" id="path" value="L:\"/>
                </p>
                <p>
                    <input type="submit" value="開(kāi)始上傳" name="submit" id="submit"/>
                </p>
            </fieldset>
        </form>
    </div>
    </body>
</html>

五、controller

package qiu.fileupload.test;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Controller
public class GetFile {
    @GetMapping("/upload")
    public String upload() {
        return "upload";
    }

    @PostMapping("/upload")
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile file,@RequestParam("path") String path) {
        if (file.isEmpty()) {
            return "上傳失敗,請(qǐng)選擇文件";
        }
        System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaa");
        String fileName = file.getOriginalFilename();
        String filePath = path;
        System.out.println(filePath);

        File dest = new File(filePath + fileName);

        try {
            file.transferTo(dest);
            return "上傳成功";
        } catch (IOException e) {
        }
        return "上傳失?。?;
    }
}

六、測(cè)試

1、瀏覽器中輸入地址:http://localhost:6555/register.html

SpringBoot文件上傳下載

2、選擇“選擇文件”,“開(kāi)始上傳”

3、到D盤(pán)D:\test\test\目錄下面查看

SpringBoot文件上傳下載

第二部分 文件下載

一、前端頁(yè)面

<!DOCTYPE html>
<html>
<head>

        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>文件下載</title>
</head>
        <body >
                <h3>文件下載列表</h3>
                <hr/>
                <div style="background-color: #F8F8FF;width: 900px;height: 800px;margin: auto;
                                                border: solid 2px blue;">
                        <div >
                                <br><br><br>
                                下載:&emsp; <a href="/MyProject/testDownload1">1.png</a> <br>
                                下載:&emsp; <a href="/MyProject/testDownload2">Android常用英語(yǔ).docx</a><br>
                                下載:&emsp; <a href="/MyProject/testDownload3">Git-2.17.0-64-bit.exe</a><br>
                                下載:&emsp; <a href="/MyProject/testDownload4">MyBatis源代碼.zip</a><br>
                        </div>
                </div>

        </body>
</html>

SpringBoot文件上傳下載

二、controller

package qiu.fileupload.test;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

@Controller
public class DownloadFile {

        @RequestMapping(value = "/testDownload1", method = RequestMethod.GET)
        public void Download1(HttpServletResponse res) {
                String fileName = "1.png";
                res.setHeader("content-type", "application/octet-stream");
                res.setContentType("application/octet-stream");
                res.setHeader("Content-Disposition", "attachment;filename=" + fileName);
                byte[] buff = new byte[1024];
                BufferedInputStream bis = null;
                OutputStream os = null;
                try {
                        os = res.getOutputStream();
                        bis = new BufferedInputStream(new FileInputStream(new File("L://test//"
                                        + fileName)));
                        int i = bis.read(buff);
                       while (i != -1) {
                                os.write(buff, 0, buff.length);
                                os.flush();
                                i = bis.read(buff);
                        }
                } catch (IOException e) {
                        e.printStackTrace();
                } finally {
                        if (bis != null) {
                                try {
                                        bis.close();
                                } catch (IOException e) {
                                        e.printStackTrace();
                                }
                        }
                }
        }

        @RequestMapping(value = "/testDownload2", method = RequestMethod.GET)
        public void Download2(HttpServletResponse res) {
                String fileName = "Android常用英語(yǔ).docx";
                res.setHeader("content-type", "application/octet-stream");
                res.setContentType("application/octet-stream");
                res.setHeader("Content-Disposition", "attachment;filename=" + fileName);
                byte[] buff = new byte[1024];
                BufferedInputStream bis = null;
                OutputStream os = null;
                try {
                        os = res.getOutputStream();
                        bis = new BufferedInputStream(new FileInputStream(new File("L://test//"
                                        + fileName)));
                        int i = bis.read(buff);
                       while (i != -1) {
                                os.write(buff, 0, buff.length);
                                os.flush();
                                i = bis.read(buff);
                        }
                } catch (IOException e) {
                        e.printStackTrace();
                } finally {
                        if (bis != null) {
                                try {
                                        bis.close();
                                } catch (IOException e) {
                                        e.printStackTrace();
                                }
                        }
                }
        }

        @RequestMapping(value = "/testDownload3", method = RequestMethod.GET)
        public void Download3(HttpServletResponse res) {
                String fileName = "Git-2.17.0-64-bit.exe";
                res.setHeader("content-type", "application/octet-stream");
                res.setContentType("application/octet-stream");
                res.setHeader("Content-Disposition", "attachment;filename=" + fileName);
                byte[] buff = new byte[1024];
                BufferedInputStream bis = null;
                OutputStream os = null;
                try {
                        os = res.getOutputStream();
                        bis = new BufferedInputStream(new FileInputStream(new File("L://test//"
                                        + fileName)));
                        int i = bis.read(buff);
                       while (i != -1) {
                                os.write(buff, 0, buff.length);
                                os.flush();
                                i = bis.read(buff);
                        }
                } catch (IOException e) {
                        e.printStackTrace();
                } finally {
                        if (bis != null) {
                                try {
                                        bis.close();
                                } catch (IOException e) {
                                        e.printStackTrace();
                                }
                        }
                }
        }

        @RequestMapping(value = "/testDownload4", method = RequestMethod.GET)
        public void Download4(HttpServletResponse res) {
                String fileName = "MyBatis源代碼.zip";
                res.setHeader("content-type", "application/octet-stream");
                res.setContentType("application/octet-stream");
                res.setHeader("Content-Disposition", "attachment;filename=" + fileName);
                byte[] buff = new byte[1024];
                BufferedInputStream bis = null;
                OutputStream os = null;
                try {
                        os = res.getOutputStream();
                        bis = new BufferedInputStream(new FileInputStream(new File("L://test//"
                                        + fileName)));
                        int i = bis.read(buff);
                       while (i != -1) {
                                os.write(buff, 0, buff.length);
                                os.flush();
                                i = bis.read(buff);
                        }
                } catch (IOException e) {
                        e.printStackTrace();
                } finally {
                        if (bis != null) {
                                try {
                                        bis.close();
                                } catch (IOException e) {
                                        e.printStackTrace();
                                }
                        }
                }
        }

}

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。

本文題目:SpringBoot文件上傳下載-創(chuàng)新互聯(lián)
當(dāng)前鏈接:http://www.muchs.cn/article30/dsjspo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、網(wǎng)站設(shè)計(jì)、云服務(wù)器網(wǎng)站設(shè)計(jì)公司、網(wǎng)站導(dǎo)航定制開(kāi)發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司