中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

SpringBoot文件上傳下載

發布時間:2020-07-30 16:04:15 來源:網絡 閱讀:6559 作者:Qiu_CJ 欄目:開發技術

第一部分 文件上傳

一、新建項目

二、配置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
# 單個文件的最大值
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=/**

四、前端頁面

前端頁面放于resources目錄下面的public目錄中,springboot會默認加載這個目錄。

<!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>用戶名:</label>
                  <input type="text"name="username"placeholder="用戶名"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="開始上傳" 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 "上傳失敗,請選擇文件";
        }
        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 "上傳失敗!";
    }
}

六、測試

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

SpringBoot文件上傳下載

2、選擇“選擇文件”,“開始上傳”

3、到D盤D:\test\test\目錄下面查看

SpringBoot文件上傳下載

第二部分 文件下載

一、前端頁面

<!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常用英語.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常用英語.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();
                                }
                        }
                }
        }

}
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

新宁县| 清水县| 宁城县| 新乡县| 七台河市| 繁峙县| 寿阳县| 大英县| 衡阳县| 平乡县| 韩城市| 鄂托克旗| 玛沁县| 长宁区| 渑池县| 武山县| 梨树县| 鹿邑县| 江津市| 元阳县| 密云县| 通化市| 瓦房店市| 四平市| 蛟河市| 木里| 伊吾县| 德江县| 西昌市| 桓台县| 樟树市| 和平区| 平邑县| 绥棱县| 平乐县| 义乌市| 澄城县| 巴彦县| 通江县| 繁峙县| 台安县|