springboot单/多文件上传和文件下载

单文件html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<meta charset="UTF-8" />
<title>Insert title here</title>
</head>
<body>
    <h1 th:inlines="text">文件上传</h1>
    <form action="fileUpload" method="post" enctype="multipart/form-data">
        <p>选择文件: <input type="file" name="file"/></p>
        <p><input type="submit" value="提交"/></p>
    </form>
</body>
</html>

多文件html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"  
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<meta charset="UTF-8" />
<title>Insert title here</title>
</head>
<body>
    <h1 th:inlines="text">文件上传</h1>
    <form action="multifileUpload" method="post" enctype="multipart/form-data" >
        <p>选择文件1: <input type="file" name="file"/></p>
        <p>选择文件2: <input type="file" name="file"/></p>
        <p>选择文件3: <input type="file" name="file"/></p>
        <p><input type="submit" value="提交"/></p>
    </form>
</body>
</html>

单文件上传Controller

@Controller
public class FileUploadController {
    
    

  
    /**
     * 实现文件上传
     * */
    @RequestMapping("fileUpload")
    @ResponseBody 
    public String fileUpload(@RequestParam("file") MultipartFile file){
    
    
        if(file.isEmpty()){
    
    
            return "false";
        }
        String fileName = file.getOriginalFilename();
        int size = (int) file.getSize();
        System.out.println(fileName + "-->" + size);
        
        String path = "F:/test" ;
        File dest = new File(path + "/" + fileName);
        if(!dest.getParentFile().exists()){
    
     //判断文件父目录是否存在
            dest.getParentFile().mkdir();
        }
        try {
    
    
            file.transferTo(dest); //保存文件
            return "true";
        } catch (IllegalStateException e) {
    
    
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "false";
        } catch (IOException e) {
    
    
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "false";
        }
    }

}

多文件上传Controller

/**
     * 实现多文件上传
     * */
    @PostMapping("multifileUpload") 
    @ResponseBody 
    public String multifileUpload(HttpServletRequest request){
    
    
        
        List<MultipartFile> files = ((MultipartHttpServletRequest)request).getFiles("fileName");
        
        if(files.isEmpty()){
    
    
            return "false";
        }

        String path = "F:/test" ;
        
        for(MultipartFile file:files){
    
    
            String fileName = file.getOriginalFilename();
            int size = (int) file.getSize();
            System.out.println(fileName + "-->" + size);
            
            if(file.isEmpty()){
    
    
                return "false";
            }else{
    
            
                File dest = new File(path + "/" + fileName);
                if(!dest.getParentFile().exists()){
    
     //判断文件父目录是否存在
                    dest.getParentFile().mkdir();
                }
                try {
    
    
                    file.transferTo(dest);
                }catch (Exception e) {
    
    
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    return "false";
                } 
            }
        }
        return "true";
    }
}

可能会出现文件超大的问题

在application.yml中声明最大文件大小限制即可

spring:
	servlet:
    	multipart:
      		max-file-size: 10MB
      		max-request-size: 100MB

或者创建一个配置类,在配置类中声明

文件的下载

前端(url下载):
在这里插入图片描述
后端:

	@GetMapping("download")
    @ResponseBody
    public String downloadDrawings(HttpServletRequest request,HttpServletResponse response,String path) throws IOException {
    
    
        if (path != null) {
    
    
            File file = new File(path);
            String fileName = file.getName();
            if (file.exists()) {
    
    
                response.setContentType("application/octet-stream");//
                response.setHeader("content-type", "application/octet-stream");
                response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
    
    
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
    
    
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                    System.out.println("success");
                } catch (Exception e) {
    
    
                    e.printStackTrace();
                } finally {
    
    
                    if (bis != null) {
    
    
                        try {
    
    
                            bis.close();
                        } catch (IOException e) {
    
    
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
    
    
                        try {
    
    
                            fis.close();
                        } catch (IOException e) {
    
    
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return null;
    }

猜你喜欢

转载自blog.csdn.net/qq_25905159/article/details/113858815