SpringBoot学习之文件上传(一)

单文件上传

在static中新建upload.html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form action="/upload" method="post" enctype="multipart/form-data">
<!--        name这里必须和后台接收名字相同-->
        <input type="file" name="file">
        <input type="submit" value="上传">
    </form>
</body>
</html>

新建FileUploadController类

package org.hx.springboot_uploadfile_demo17;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RestController
public class FileUploadController {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("/yyyy/MM/dd/");
    @PostMapping("/upload")
    public String upload(MultipartFile file, HttpServletRequest request){
        String realpath = request.getServletContext().getRealPath("/");
        String format = simpleDateFormat.format(new Date());
        String path = realpath + format;
        File folder = new File(path);
        if (!folder.exists())
        {
            folder.mkdirs();
        }
        String oldName = file.getOriginalFilename();
        String newName = UUID.randomUUID().toString()+oldName.substring(oldName.lastIndexOf("."));
        try {
            // 将上传的文件写入到指定的位置
            file.transferTo(new File(folder,newName));
            String s = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+format+newName;
            return  s;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
}

多文件上传(一)

新建uploads.html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form action="/uploads" method="post" enctype="multipart/form-data">
        <input type="file" name="files" multiple>
        <input type="submit" value="上传">
    </form>
</body>
</html>

新建FileUploadsController类

package org.hx.springboot_uploadfile_demo17;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RestController
public class FileUploadsController {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("/yyyy/MM/dd/");
    @PostMapping("/uploads")
    public void upload(MultipartFile[] files, HttpServletRequest request){
        String realpath = request.getServletContext().getRealPath("/");
        String format = simpleDateFormat.format(new Date());
        String path = realpath + format;
        File folder = new File(path);
        if (!folder.exists())
        {
            folder.mkdirs();
        }
        try {
            for (MultipartFile file : files) {
                String oldName = file.getOriginalFilename();
                String newName = UUID.randomUUID().toString()+oldName.substring(oldName.lastIndexOf("."));
                file.transferTo(new File(folder,newName));
                String url = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+format+newName;
                System.out.println(url);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

多文件上传(二)

新建uploads2.html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form action="/uploads2" method="post" enctype="multipart/form-data">
        <input type="file" name="file1" >
        <input type="file" name="file2" >
        <input type="submit" value="上传">
    </form>
</body>
</html>

新建FileUploadsController类

package org.hx.springboot_uploadfile_demo17;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RestController
public class FileUploads2Controller {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("/yyyy/MM/dd/");
    @PostMapping("/uploads2")
    public void upload(MultipartFile file1,MultipartFile file2, HttpServletRequest request){
        String realpath = request.getServletContext().getRealPath("/");
        String format = simpleDateFormat.format(new Date());
        String path = realpath + format;
        File folder = new File(path);
        if (!folder.exists())
        {
            folder.mkdirs();
        }
        try {
            String oldName1 = file1.getOriginalFilename();
            String newName1 = UUID.randomUUID().toString()+oldName1.substring(oldName1.lastIndexOf("."));
            file1.transferTo(new File(folder,newName1));
            String url1 = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+format+newName1;
            System.out.println(url1);

            String oldName2 = file2.getOriginalFilename();
            String newName2 = UUID.randomUUID().toString()+oldName2.substring(oldName2.lastIndexOf("."));
            file2.transferTo(new File(folder,newName2));
            String url2 = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+format+newName2;
            System.out.println(url2);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值