البث عند التنزيل
في هذا الدرس 4 موضوعات
هذا الدرس مخطط له. يعرض المخطط أدناه الموضوعات التي سيغطيها، ولم يُنشر الدرس الكامل بعد.
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
في هذا الدرس
send big files without loading them into memory.
الموضوعات المخطط لها
- Reading a whole file into a
byte[]uses memory per request; ten parallel downloads can crash the server. - Return a
Resource(for exampleFileSystemResource) or aStreamingResponseBody; Spring copies it in chunks. - Set
Content-Typefrom your stored record, andContent-Lengthwhen known. Content-Disposition: attachment; filename="..."makes browsers download;inlinedisplays.- Return
404if the file record or file is missing.
تفاصيل المخطط المبدئي
عرض عملي
@GetMapping("/images/{id}")
ResponseEntity<Resource> download(@PathVariable String id) {
Resource r = storage.load(id);
return ResponseEntity.ok().contentType(MediaType.IMAGE_PNG).body(r);
}اختبر فهمك
Why not return a byte[]?
أظهر الإجابة
The whole file sits in memory for each request.
التطبيق
طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.
تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.