File Download এর জন্য ResponseEntity ব্যবহার

Spring MVC এর মধ্যে File Upload এবং Download - স্প্রিং এমভিসি (Spring MVC) - Java Technologies

358

Spring MVC তে File Download ফিচার বাস্তবায়ন করার জন্য ResponseEntity খুবই কার্যকরী একটি উপাদান। এটি HTTP রেসপন্স কন্ট্রোল করতে এবং কাস্টম হেডার সহ ফাইল রিটার্ন করতে সহায়ক। ResponseEntity ব্যবহারের মাধ্যমে আপনি ফাইলের কন্টেন্ট, কন্টেন্ট টাইপ, কন্টেন্ট ডিসপোজিশন ইত্যাদি নির্দিষ্ট করতে পারেন।

নিচে Spring MVC তে File Download করার জন্য ResponseEntity ব্যবহার করার একটি উদাহরণ দেওয়া হলো।


Step 1: Maven Dependency

প্রথমে, আপনার pom.xml ফাইলে Spring Web এবং অন্য প্রয়োজনীয় ডিপেনডেন্সি যুক্ত করুন:

<dependencies>
    <!-- Spring Boot Starter Web for RESTful APIs -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    <!-- For File IO Support (optional, if not using Spring Boot) -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
    </dependency>
</dependencies>

Step 2: Controller Class for File Download

এখন, একটি কন্ট্রোলার তৈরি করুন যা ফাইল ডাউনলোড রিকুয়েস্ট হ্যান্ডেল করবে এবং ResponseEntity ব্যবহার করে ফাইল রিটার্ন করবে।

package com.example.controller;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.springframework.core.io.InputStreamResource;

@RestController
public class FileDownloadController {

    @GetMapping("/download/{filename}")
    public ResponseEntity<InputStreamResource> downloadFile(@PathVariable String filename) throws IOException {
        // File location
        File file = new File("path/to/your/files/" + filename);

        if (!file.exists()) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }

        // Set up the file input stream
        InputStream inputStream = new FileInputStream(file);

        // Prepare headers for the response
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attachment; filename=" + file.getName());
        headers.add("Content-Type", MediaType.APPLICATION_OCTET_STREAM_VALUE); // or use specific media type like image/jpg
        headers.add("Content-Length", String.valueOf(file.length()));

        // Return the file as an InputStreamResource wrapped in ResponseEntity
        return ResponseEntity.ok()
                .headers(headers)
                .body(new InputStreamResource(inputStream));
    }
}

Explanation:

  • @RestController: Spring MVC কন্ট্রোলার হিসেবে @RestController অ্যানোটেশন ব্যবহার করা হয়েছে যা স্বয়ংক্রিয়ভাবে JSON বা স্ট্রিম রিটার্ন করে।
  • @GetMapping("/download/{filename}"): এটি HTTP GET রিকুয়েস্ট হ্যান্ডেল করবে যা ফাইল ডাউনলোডের জন্য ব্যবহৃত হবে।
  • InputStreamResource: এটি একটি Spring ক্লাস যা ফাইলের কন্টেন্ট রিড করার জন্য InputStream গ্রহণ করে এবং রেসপন্সের অংশ হিসেবে সরবরাহ করে।
  • Content-Disposition: HTTP হেডারে ফাইলের নাম নির্ধারণ করতে ব্যবহৃত হয়, যাতে এটি ব্রাউজারে ডাউনলোড হিসেবে প্রদর্শিত হয়।
  • MediaType.APPLICATION_OCTET_STREAM_VALUE: এটি একটি সাধারণ MIME টাইপ যা ফাইল ডাউনলোডের জন্য ব্যবহৃত হয়।

Step 3: Sample File for Download

ধরি, আমাদের ফাইলটি C:/files/sample.pdf এ অবস্থিত। ফাইলটি ডাউনলোড করার জন্য আপনি URL /download/sample.pdf এ GET রিকোয়েস্ট পাঠাতে পারবেন।


Step 4: ResponseEntity and File Download Example

Request Example:

GET /download/sample.pdf HTTP/1.1
Host: localhost:8080

Response Example:

HTTP/1.1 200 OK
Content-Type: application/octet-stream
Content-Disposition: attachment; filename="sample.pdf"
Content-Length: 123456

<binary content of the file>
  • Content-Disposition: এটিতে attachment ব্যবহৃত হয়েছে, যাতে ব্রাউজারটি ফাইলটি ডাউনলোড হিসেবে প্রদর্শন করে।
  • Content-Type: এখানে application/octet-stream ব্যবহার করা হয়েছে, যা ফাইলটি একটি বাইনরি স্ট্রিম হিসেবে পাঠানোর নির্দেশ দেয়।

Step 5: Testing the Endpoint

  1. Run the Spring Boot application.
  2. Access the file download URL in the browser or using a tool like Postman or cURL:

    GET http://localhost:8080/download/sample.pdf
    

    এটি ব্রাউজারে অথবা Postman এ ফাইল ডাউনলোড শুরু করবে।


Conclusion

Spring MVC তে File Download করার জন্য ResponseEntity খুবই শক্তিশালী এবং কাস্টমাইজযোগ্য একটি উপাদান। InputStreamResource এবং HttpHeaders ব্যবহার করে আপনি ফাইলের কন্টেন্ট, কন্টেন্ট টাইপ, কন্টেন্ট ডিসপোজিশন সহ সমস্ত ডাউনলোড সম্পর্কিত তথ্য কাস্টমাইজ করতে পারেন। Spring Boot প্রজেক্টে এটি খুব সহজে ব্যবহার করা যায়।

Content added By
Promotion

Are you sure to start over?

Loading...