Learn / Frameworks / Spring Boot / Uploads and Scheduling

Spring Boot · Lesson 12 of 15

Uploads and Scheduling

Multipart files, size limits and @Scheduled jobs.

  • Advanced
  • 14 min read
  • 3 objectives

Before this lessonLesson 11: OpenAPI with springdoc

What you will learn

  • Save an upload
  • Cap file size
  • Run a cron method

Your Progress

0 of 15 lessons 0%

  • Lessons0 / 15
  • Completed0
  • Est. time left~ 4 hours

Create a free account to keep your progress on every device.

Two small but common features: receiving a file and running a method on a schedule.

Multipart

@PostMapping(value = "/api/files", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public Map<String, String> upload(@RequestParam("file") MultipartFile file) throws IOException {
    if (file.isEmpty()) throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
    String name = UUID.randomUUID() + "-" + file.getOriginalFilename();
    Path dest = Path.of(uploadDir, name);
    file.transferTo(dest);
    return Map.of("name", name);
}
spring:
  servlet:
    multipart:
      max-file-size: 5MB
      max-request-size: 5MB

Scheduling

@Configuration
@EnableScheduling
public class ScheduleConfig {}

@Component
public class PurgeJob {
    @Scheduled(cron = "0 15 3 * * *")   // 03:15 every day
    public void purgeExpired() { ... }
}
Up next · Lesson 13Domain Events and MessagingApplication events in-process and a first look at a message broker.