উদাহরণ সহ File Upload এবং Download

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

315

Spring MVC তে File Upload এবং File Download পরিচালনা করা সহজ এবং বেশ কার্যকরী। আপনি ব্যবহারকারী থেকে ফাইল গ্রহণ করতে পারেন এবং সেই ফাইল সার্ভারে সংরক্ষণ করতে পারেন অথবা ক্লায়েন্টকে ফাইল পাঠাতে পারেন।

এই উদাহরণে, আমরা Spring MVC ব্যবহার করে একটি সিম্পল ফাইল আপলোড এবং ডাউনলোড অ্যাপ্লিকেশন তৈরি করব।


Step 1: Maven Dependencies

Spring MVC ফাইল আপলোড এবং ডাউনলোডের জন্য প্রয়োজনীয় ডিপেনডেন্সি যোগ করুন।

<dependencies>
    <!-- Spring Boot Starter Web for MVC functionality -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- Spring Boot Starter Thymeleaf for rendering views -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

    <!-- Spring Boot Starter for file handling -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <!-- Apache Commons FileUpload (For handling file uploads) -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.4</version>
    </dependency>

    <!-- Spring Boot Starter Validation for form validation -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>

    <!-- Spring Boot Starter Test for testing -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
</dependencies>
  • spring-boot-starter-web: Spring MVC ওয়েব অ্যাপ্লিকেশন তৈরি করার জন্য।
  • commons-fileupload: ফাইল আপলোডের জন্য প্রয়োজনীয় লাইব্রেরি।

Step 2: Create File Upload Controller

Spring MVC কন্ট্রোলারে ফাইল আপলোড এবং ডাউনলোডের জন্য MultipartFile ব্যবহার করা হয়। এখানে আমরা একটি ফাইল আপলোডের জন্য কন্ট্রোলার তৈরি করব।

FileUploadController.java:

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import java.io.File;
import java.io.IOException;

@Controller
public class FileUploadController {

    private static String UPLOADED_FOLDER = "uploaded_files/";

    // Method to show the file upload form
    @GetMapping("/upload")
    public String showUploadForm() {
        return "uploadForm"; // Display the form to upload a file
    }

    // Method to handle file upload
    @PostMapping("/upload")
    public String handleFileUpload(@RequestParam("file") MultipartFile file, Model model) {
        if (file.isEmpty()) {
            model.addAttribute("message", "Please select a file to upload");
            return "uploadForm";
        }

        try {
            // Save the file on the server
            File dest = new File(UPLOADED_FOLDER + file.getOriginalFilename());
            file.transferTo(dest);

            model.addAttribute("message", "File uploaded successfully: " + file.getOriginalFilename());
        } catch (IOException e) {
            model.addAttribute("message", "Failed to upload file: " + e.getMessage());
        }

        return "uploadForm"; // Display upload result message
    }

    // Method to download a file
    @GetMapping("/download/{filename}")
    @ResponseBody
    public byte[] downloadFile(@PathVariable("filename") String filename) throws IOException {
        File file = new File(UPLOADED_FOLDER + filename);
        return java.nio.file.Files.readAllBytes(file.toPath());
    }
}
  • @RequestParam("file") MultipartFile file: ফর্ম থেকে আপলোড করা ফাইল গ্রহণ করার জন্য ব্যবহৃত হয়।
  • file.transferTo(dest): ফাইলটি সার্ভারে সেভ করার জন্য।
  • @GetMapping("/download/{filename}"): নির্দিষ্ট ফাইল ডাউনলোড করার জন্য ব্যবহৃত রিকোয়েস্ট।

Step 3: Create Upload Form View (uploadForm.html)

এখন, ফাইল আপলোড ফর্ম তৈরি করুন যাতে ব্যবহারকারী ফাইল সিলেক্ট করতে পারে এবং ফাইলটি সার্ভারে আপলোড করতে পারে।

uploadForm.html (Thymeleaf Template):

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>File Upload</title>
</head>
<body>
    <h2>Upload File</h2>
    <form action="/upload" method="post" enctype="multipart/form-data">
        <input type="file" name="file" />
        <button type="submit">Upload</button>
    </form>

    <p th:if="${message}" th:text="${message}"></p>

    <h3>Download File:</h3>
    <form action="/download/{filename}" method="get">
        <input type="text" name="filename" placeholder="Enter filename to download" />
        <button type="submit">Download</button>
    </form>
</body>
</html>
  • <form action="/upload" method="post" enctype="multipart/form-data">: ফাইল আপলোড ফর্মের জন্য multipart/form-data এনকোডিং ব্যবহার করা হয়।
  • ${message}: যদি ফাইল আপলোড সফল হয় বা ব্যর্থ হয়, তবে ইউজারের জন্য একটি বার্তা দেখানো হবে।

Step 4: Application Class

Spring Boot অ্যাপ্লিকেশন ক্লাস তৈরি করুন, যেখানে আমরা Spring Boot রান করব।

FileUploadApplication.java:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class FileUploadApplication {
    public static void main(String[] args) {
        SpringApplication.run(FileUploadApplication.class, args);
    }
}
  • @SpringBootApplication: Spring Boot অ্যাপ্লিকেশন চালু করার জন্য প্রধান অ্যানোটেশন।

Step 5: File Upload Directory

আপনার সার্ভার ফোল্ডারে একটি ফোল্ডার তৈরি করুন যেখানে ফাইল আপলোড করা হবে। উদাহরণস্বরূপ: uploaded_files/


Step 6: Run the Application

  1. Spring Boot অ্যাপ্লিকেশন চালু করুন: mvn spring-boot:run অথবা আপনার IDE থেকে FileUploadApplication.java ক্লাস চালিয়ে অ্যাপ্লিকেশন রান করুন।
  2. ফাইল আপলোড ফর্মে যান: http://localhost:8080/upload URL এ যান।
  3. ফাইল আপলোড করুন: ফাইল নির্বাচন করে Upload বাটনে ক্লিক করুন।
  4. ফাইল ডাউনলোড করুন: ফাইল নাম দিয়ে ডাউনলোড বাটনে ক্লিক করুন, যেমন: /download/yourfile.txt.

Step 7: Testing

  1. ফাইল আপলোড: একটি ফাইল আপলোড করার পর যদি সফল হয়, তবে "File uploaded successfully" বার্তা দেখাবে।
  2. ফাইল ডাউনলোড: আপনি যেকোনো আপলোড করা ফাইল ডাউনলোড করতে পারেন filename ইনপুট দিয়ে।

Conclusion:

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

Content added By
Promotion

Are you sure to start over?

Loading...