真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

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

第一部分 文件上傳

一、新建項(xiàng)目

二、配置maven



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.2.RELEASE
         
    
    qiu
    fileupload
    0.0.1-SNAPSHOT
    fileupload
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

三、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ù)用戶設(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è)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。


    
        
        文件上傳
        
    
    
    
文件上傳

五、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è)面





        
        文件下載

        
                

文件下載列表





下載:  1.png
下載:  Android常用英語(yǔ).docx
下載:  Git-2.17.0-64-bit.exe
下載:  MyBatis源代碼.zip

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ù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。


文章題目:SpringBoot文件上傳下載-創(chuàng)新互聯(lián)
轉(zhuǎn)載注明:http://weahome.cn/article/dsjspo.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部