简介
本文主要介绍基于SpringBoot搭建的Web服务,对于文本文件(txt)和文本文件压缩包(gzip)的下载操作。
下载操作
由于我们主要介绍文本文件的下载操作,将重点集中在Controller中方法的实现,对于SpringBoot搭建Web服务就不再介绍。
基于SpringBoot搭建Web服务可参考另外一篇博文:基于SpringBoot搭建Web服务
下载操作包含3个类型
- 将字符串写入到文件中提供下载
- 下载本地文本文件
- 本地文本文件转成GZIP压缩包下载
Controller
新建DownloadController.class
添加@RestController
注解,代码如下:
package hello.controller;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;
/**
* Created on 2018/12/20.
*
* @author Marvin Yang
*/
@RestController
public class DownloadController {
}
将字符串写入到文件中下载
在DownloadController.class
中添加方法downloadFile
实现将字符串写入到文件中并提供下载操作。有两点需要注意,(1) 函数无返回值;(2) 选择HttpServletResponse
作为返回。
/**
* txt文件下载接口
* 1. 将字符串写入到文件中提供下载
*
* @param response a HttpServletResponse
*/
@RequestMapping(value = "/download", method = RequestMethod.GET)
public void downloadFile(HttpServletResponse response) {
// set header
response.setHeader("Content-Disposition", "attachment; filename=demo.txt");
String demo = "Here is a demo of download file.";
try {
response.getOutputStream().write(demo.getBytes());
response.flushBuffer();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
在浏览器请求,测试样例如下:
# 8080是web端口
http://localhost:8080/download?
下载本地文本文件
在DownloadController.class
中添加方法downloadLocalFile
实现本地文本文件的下载操作。有3点需要注意,(1) URL调用方式为/download/{fileName}
; (2) InputStream
必须close()
,否则一旦接口被调用,该文件在本地则不允许删除,除非关闭web服务; (3) 注意本地文件的路径,保证可以获取到。
/**
* 下载本地文本文件
* 1. FileInputStream必须close, 否则会被占用, 导致如果web服务不关闭文件无法被本地删除
*
* @param fileName 本地文本文件名称
* @param response a HttpServletResponse
*/
@RequestMapping(value = "/download/{fileName}", method = RequestMethod.GET)
public void downloadLocalFile(@PathVariable("fileName") String fileName, HttpServletResponse response) {
// set header
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
FileInputStream inputStream = null;
try {
File file = new File(fileName);
if (!file.exists()) {
String errorMsg = "Can not find such file: " + fileName;
response.getOutputStream().write(errorMsg.getBytes());
} else {
inputStream = new FileInputStream(fileName);
IOUtils.copy(inputStream, response.getOutputStream());
}
response.flushBuffer();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException ignore) {}
}
}
}
在浏览器请求,测试样例如下:
# 8080是web端口
# demo_local是本地文件名称(注意文件路径)
http://localhost:8080/download/demo_local?
本地文本文件转成GZIP压缩包下载
在DownloadController.class
中添加方法downloadLocalFile
实现本地文本文件读取并转换为gzip下载。有2点需要注意,(1) 设置content-type; (2) response.getOutputStream()
需要使用GZIPOutputStream
/**
* 本地文本文件转成GZIP压缩包下载
* 1. FileInputStream必须close, 否则会被占用, 导致如果web服务不关闭文件无法被本地删除
*
* @param fileName 本地文本文件名称
* @param response HttpServletResponse
*/
@RequestMapping(value = "/download/zip/{fileName}", method = RequestMethod.GET)
public void downloadLocalGZip(@PathVariable("fileName") String fileName, HttpServletResponse response) {
// set header
response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".gz");
response.setContentType("application/zip");
FileInputStream inputStream = null;
try (GZIPOutputStream outputStream = new GZIPOutputStream(response.getOutputStream())){
File file = new File(fileName);
if (!file.exists()) {
String errorMsg = "can not find such file: " + fileName;
response.getOutputStream().write(errorMsg.getBytes());
} else {
inputStream = new FileInputStream(fileName);
IOUtils.copy(inputStream, outputStream);
}
response.flushBuffer();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException ignore) {}
}
}
}
在浏览器请求,测试样例如下:
# 8080是web端口
# demo_local是本地文件名称(注意文件路径)
http://localhost:8080/download/zip/demo_local?
总结
我们介绍了RestController
的service
服务提供文本文件和gzip压缩包的下载操作。
如果是读取其他类型的文件,则尝试修改InputStream
的类型完成文件读取;如果是zip类型的文件下载,则可以使用java.util.zip.ZipOutputStream
,注意zip的操作不同于gzip操作。