文件流返回指的是將指定文件通過HTTP返回給客戶端瀏覽器,並且支持文件的下載、打開與預覽等操作。在Java中,實現文件流返回的方案有多種,下面將從不同的角度進行詳細闡述。
一、使用HttpServletResponse實現文件流返回
在Java Web應用開發中,可以使用HttpServletResponse對象的OutputStream屬性向客戶端瀏覽器輸出文件流內容,從而實現文件的返回。具體代碼示例如下:
@RequestMapping("/downloadFile")
public void downloadFile(HttpServletRequest request, HttpServletResponse response) throws IOException {
String filePath = "D:/example.pdf";
String fileName = "example.pdf";
File file = new File(filePath);
response.setHeader("content-type", "application/octet-stream");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
byte[] buff = new byte[1024];
BufferedInputStream bis = null;
OutputStream os = null;
try {
os = response.getOutputStream();
bis = new BufferedInputStream(new FileInputStream(file));
int i = bis.read(buff);
while (i != -1) {
os.write(buff, 0, i);
i = bis.read(buff);
}
os.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
bis.close();
}
}
}
二、使用Spring MVC提供的文件下載功能實現文件流返回
在Spring MVC框架中,提供了方便的文件下載功能。只需要在Controller方法上添加ResponseEntity類型方法返回值即可,具體代碼示例如下:
@RequestMapping("/downloadFile")
public ResponseEntity downloadFile(HttpServletRequest request) throws IOException {
String filePath = "D:/example.pdf";
String fileName = "example.pdf";
HttpHeaders headers = new HttpHeaders();
headers.setContentDispositionFormData("attachment", fileName);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
File file = new File(filePath);
byte[] bytes = FileUtils.readFileToByteArray(file);
return new ResponseEntity(bytes, headers, HttpStatus.OK);
}
三、使用Apache Commons IO提供的文件下載功能實現文件流返回
除了Spring MVC提供的文件下載功能外,也可以使用Apache Commons IO提供的文件下載功能實現文件流返回。具體代碼示例如下:
@RequestMapping("/downloadFile")
public void downloadFile(HttpServletRequest request, HttpServletResponse response) throws IOException {
String filePath = "D:/example.pdf";
String fileName = "example.pdf";
response.setHeader("content-type", "application/octet-stream");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
File file = new File(filePath);
InputStream inputStream = FileUtils.openInputStream(file);
OutputStream outputStream = response.getOutputStream();
IOUtils.copy(inputStream, outputStream);
outputStream.flush();
outputStream.close();
inputStream.close();
}
四、使用Servlet API提供的文件下載功能實現文件流返回
Servlet API提供了文件下載功能的實現方式,具體代碼示例如下:
@RequestMapping("/downloadFile")
public void downloadFile(HttpServletRequest request, HttpServletResponse response) throws IOException {
String filePath = "D:/example.pdf";
String fileName = "example.pdf";
response.setHeader("content-type", "application/octet-stream");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
File file = new File(filePath);
FileInputStream inputStream = new FileInputStream(file);
ServletOutputStream outputStream = response.getOutputStream();
byte[] buffer = new byte[1024];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
inputStream.close();
outputStream.close();
}
五、代碼實現對比分析
從上述不同實現方案的代碼示例中,可以看出相對來說,使用Spring MVC提供的文件下載功能代碼相對簡潔明了,但是需要引入Spring MVC相關依賴。使用Apache Commons IO提供的文件下載功能相對來說代碼較為簡單,但依賴Apache Commons IO庫。使用HttpServletResponse實現文件流返回代碼較為複雜,但是效率相對來說最高。如果項目中已經引入Servlet API相關依賴,可以選擇使用Servlet API提供的文件下載功能。
綜上所述,根據各自項目特點和需求,選擇不同的文件流返回實現方式,既能夠滿足需求,也能夠保證代碼的可讀性和簡潔性。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/284558.html
微信掃一掃
支付寶掃一掃