java 实现文件下载的功能, 用代码举例至少3个案例

本文提供了三种在Java中实现文件下载功能的方法,包括使用ServletAPI、ApacheCommonsIO库和SpringBoot框架,以帮助开发者根据项目需求选择合适的方案。
摘要由CSDN通过智能技术生成

Certainly, here are three examples of how you can implement file download functionality in Java:

1.Using Servlet API:


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/download")
public class FileDownloadServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String filePath = "path/to/your/file";
        File downloadFile = new File(filePath);
        FileInputStream inputStream = new FileInputStream(downloadFile);
        String mimeType = getServletContext().getMimeType(filePath);
        if (mimeType == null) {
            mimeType = "application/octet-stream";
        }
        response.setContentType(mimeType);
        response.setContentLength((int) downloadFile.length());
        String headerKey = "Content-Disposition";
        String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName());
        response.setHeader(headerKey, headerValue);
        int bytesRead;
        while ((bytesRead = inputStream.read()) != -1) {
            response.getOutputStream().write(bytesRead);
        }
        inputStream.close();
    }
}


2.Using Apache Commons IO:

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/download")
public class FileDownloadServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String filePath = "path/to/your/file";
        File downloadFile = new File(filePath);
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment; filename=\"" + downloadFile.getName() + "\"");
        FileUtils.copyFile(downloadFile, response.getOutputStream());
    }
}


3.Using Spring Boot:


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.io.FileSystemResource;
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.RestController;
import java.io.File;

@SpringBootApplication
@RestController
public class FileDownloadApplication {
    public static void main(String[] args) {
        SpringApplication.run(FileDownloadApplication.class, args);
    }

    @GetMapping("/download")
    public ResponseEntity<FileSystemResource> downloadFile() {
        String filePath = "path/to/your/file";
        File downloadFile = new File(filePath);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentDispositionFormData("attachment", downloadFile.getName());
        headers.setContentLength(downloadFile.length());
        return new ResponseEntity<>(new FileSystemResource(downloadFile), headers, HttpStatus.OK);
    }
}

These examples demonstrate how to implement file download functionality in Java using Servlet API, Apache Commons IO library, and Spring Boot framework. You can choose the approach that best fits your project requirements and preferences.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值