Springboot 上传文件大小限制

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 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> requestEntity = new HttpEntity>(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

		
			commons-fileupload
			commons-fileupload
			1.3.3
		

入口类 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;
	}

你可能感兴趣的:(springboot)