SpringBoot文件上传下载-创新互联-成都快上网建站

SpringBoot文件上传下载-创新互联

第一部分 文件上传

一、新建项目

二、配置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
# 单个文件的大值
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会默认加载这个目录。

十余年的灌云网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都全网营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整灌云建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“灌云网站设计”,“灌云网站推广”以来,每个客户项目都认真落实执行。


    
        
        文件上传
        
    
    
    
文件上传

五、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文件上传下载

第二部分 文件下载

一、前端页面





        
        文件下载

        
                

文件下载列表





下载:  1.png
下载:  Android常用英语.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常用英语.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();
                                }
                        }
                }
        }

}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页标题:SpringBoot文件上传下载-创新互联
分享网址:http://kswjz.com/article/dsjspo.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流