Guide4:文件上下传

文章来源:https://blog.csdn.net/TomCat_And_Jerry/article/details/114142098

1 创建新项目

在这里插入图片描述

2 创建html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>up-down-files</title>
</head>
<body>
<h1>文件上传</h1>
<form action="/upload" method="post" enctype="multipart/form-data">
    <input type="file" name="file"/>
    <input type="submit" value="上传"/>
</form>
<h1>文件下载</h1>
<a href="/download?fileName=pic1.png">pic1.png</a>
</body>
</html>

3 添加限制

# 指定服务器端文件大小限制
spring.servlet.multipart.max-file-size=300MB
# 指定客户端文件大小限制
spring.servlet.multipart.max-request-size=300MB

fileLocation=static/files

4 创建Controller

package com.example.up_down_files;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;

@Controller
public class FileUploadController {
    @Value("${fileLocation}")
    private String fileLocation;

    @PostMapping("upload")
    @ResponseBody
    private void upload(MultipartFile file) throws IOException {
        System.out.println(file.getOriginalFilename());//fileName
        System.out.println(file.getContentType());//fileType
        System.out.println(file.getSize());//fileSize
        System.out.println(file.getInputStream());//inputStream

        // 获取web应用中files文件夹的绝对路径
        String realPath = ResourceUtils.getURL("classpath:").getPath()+fileLocation;
        System.out.println(fileLocation);
        System.out.println(realPath);
        File newFile = new File(realPath);
        if(!newFile.exists()){
            newFile.mkdirs();
        }
        Date date = new Date();
        String filename = date.getTime()+"@"+file.getOriginalFilename();
        // 上传
        file.transferTo(new File(newFile,filename));
    }

    @GetMapping("download")
    public void download(String fileName, HttpServletResponse response) throws IOException {
        // 获取待下载文件所在文件夹的绝对路径
        String realPath = ResourceUtils.getURL("classpath:").getPath()+fileLocation;
        System.out.println(fileLocation);
        // 获得文件输入流
        FileInputStream inputStream = new FileInputStream(new File(realPath,fileName));
        // 设置响应头、以附件形式打开文件
        response.setHeader("content-disposition","attachment;fileName="+fileName);
        ServletOutputStream outputStream = response.getOutputStream();
        int len = 0;
        byte[] data = new byte[1024];
        while((len = inputStream.read(data))!=-1){
            outputStream.write(data,0,len);
        }
        outputStream.close();
        inputStream.close();
    }
}

5 运行类

package com.example.up_down_files;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class UpDownFilesApplication {
    public static void main(String[] args) {
        SpringApplication.run(UpDownFilesApplication.class, args);
    }
}

6 运行结果

在这里插入图片描述
在这里插入图片描述

输出结果:
icon2.png
image/png
231891
java.io.FileInputStream@df7775c
static/files
/D:/JavaIdeaProjects/aispringboot-1/up_down_files/target/classes/static/files

7 目录结构

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值