Springboot 上传文件大小限制

本文介绍如何在SpringBoot中实现文件上传功能,包括解除上传文件大小限制、使用FastDFS文件系统存储文件,并通过TDFileManagement实体类进行文件管理。同时,提供了前端模拟上传文件的示例。
摘要由CSDN通过智能技术生成

Springboot 上传文件大小限制

server Controller层

    @PostMapping("upload")
    @ResponseBody
    public ResponseBean upload(@RequestParam("file") MultipartFile file){
        String fileName = file.getOriginalFilename();
        String fileExtName = fileName.substring(fileName.lastIndexOf(".")+1);
         //上传至fastdfs文件系统
        String fileId = fastDFSClient.uploadFile(file);

        if(StringUtils.isEmpty(fileId)){
            Log.error(logger,"上传文件服务器失败,fileName:{}",fileName);
            return new ResponseBean("上传至文件服务器失败",RestApi.Code.FAIL,null);
        }
		//上传文件进行管理
        int userId = 0;
        TDFileManagement fileManagement = new TDFileManagement();
        fileManagement.setCreateMan(userId);
        fileManagement.setFileId(fileId);
        fileManagement.setModifyMan(userId);
        fileManagement.setFileType(fileExtName);
        fileManagement.setFileName(fileName);
        fileManagement.setFileGroup(group);
        fileManagement.setIsDueTime(defauleIsDueTime);
        if(isDueTime()){
            fileManagement.setDueTime(getDueTime());
        }
        fileManagement.setStatus("1");
        itdFileManagementService.insertTDFileManagement(fileManagement);
        return new ResponseBean(RestApi.Msg.SUCCESS,RestApi.Code.SUCCESS,fileManagement);
    }

模拟前端上传文件

    @PostMapping("streamUpload")
    @ResponseBody
    public ResponseBean streamUpload(@RequestJson(name = "fileName",required = true)String fileName,
                                     @RequestJson(name = "fileExtName",required = true)String fileExtName,
                                    @RequestJson(name = "context",required = true)String context) throws IOException {
        RestTemplate restTemplate = new RestTemplate();
        String url  = "http://localhost:8080/file/upload";
        File file = null;
        BufferedWriter out = null;
        MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
        HttpHeaders headers = new HttpHeaders();
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        try{
            file = File.createTempFile(fileName,fileExtName);

            out = new BufferedWriter(new FileWriter(file));
            out.write(context);
            out.write(context);
            out.write(context);
            out.flush();
            FileSystemResource fileSystemResource = new FileSystemResource(file);
            param.add("file",fileSystemResource);
            HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(param,
                    headers);
            return restTemplate.postForObject(url,requestEntity,ResponseBean.class);
        } catch (Exception e){
            e.printStackTrace();
            return new ResponseBean(RestApi.Msg.FAIL,RestApi.Code.FAIL,e.getMessage());
        } finally {
            if(out != null){
                out.close();
            }
            if(file != null){
                file.deleteOnExit();
            }
        }
    }

pom.xml

		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.3.3</version>
		</dependency>

入口类 SpringBootApplication

在启动类上添加注解

@EnableAutoConfiguration(exclude = {MultipartAutoConfiguration.class})

注入Bean

    @Bean(name = "multipartResolver")
	public CommonsMultipartResolver multipartResolver(){
		CommonsMultipartResolver resolver = new CommonsMultipartResolver();
		resolver.setDefaultEncoding("utf-8");
		//解除上传文件的大小的限制
		resolver.setMaxInMemorySize(-1);
		return resolver;
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值