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.