写一个controller service,下载springboot项目中的resource下的upload文件夹下的load.txt,本人之前没接触过,谢谢大牛写的详细点!!!
这个题的有两个难点:
1、读取资源文件,下载文件;
2、spring boot controller service的写法。
一、Controller Service代码如下:
package com.qianqiangongzi.controller;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.qianqiangongzi.utils.DownloadResourceFileUtils;
/**
* 下载资源文件接口
*
* @author 谦谦公子爱编程
*
*/
@RestController
@RequestMapping(value = "/api/DownloadResourceFile")
public class DownloadResourceFileController {
/**
* 下载资源文件
*
* @param response
* @throws Exception
*/
@RequestMapping(value = "/downloadFile", method = RequestMethod.GET)
public void downloadFile(HttpServletResponse response) throws Exception {
DownloadResourceFileUtils.downloadFile(response);
}
}
二、下载文件工具类
package com.qianqiangongzi.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ResourceUtils;
/**
* 下载资源文件
*
* @author 谦谦公子爱编程
*
*/
public class DownloadResourceFileUtils {
public static final String INLINE = "inline"; // 在线打开
public static final String ATTACHMENT = "attachment"; // 附件下载
public static final Logger logger = LoggerFactory.getLogger(DownloadResourceFileUtils.class);
public static void downloadFile(HttpServletResponse response) throws Exception {
// 下载文件部分路径
String fileName = "upload\\load.txt";
// 获取文件根路径
String basePath = getResourceBasePath();
// 获取文件路径
File downloadFile = new File(basePath, fileName);
// 下载问价
downloadFile(response, downloadFile.getAbsolutePath(), downloadFile.getName(), ATTACHMENT);
}
/**
* 获取项目根路径
*
* @return
*/
private static String getResourceBasePath() {
// 获取跟目录
File path = null;
try {
path = new File(ResourceUtils.getURL("classpath:").getPath());
} catch (FileNotFoundException e) {
// nothing to do
}
if (path == null || !path.exists()) {
path = new File("");
}
String pathStr = path.getAbsolutePath();
// 如果是在eclipse中运行,则和target同级目录,如果是jar部署到服务器,则默认和jar包同级
pathStr = pathStr.replace("\\target\\classes", "");
pathStr += "\\src\\main\\resources";
return pathStr;
}
/*
* 下载文件,需要文件绝对路径
*
* @param response 写回流对象
*
* @param filePath 文件的绝对路径
*
* @param fileName 下载时的文件名称
*
* @param downloadWay 下载方式INLINE(在线打开) or ATTACHMENT(附件下载)
*
* @throws Exception
*/
private static void downloadFile(HttpServletResponse response, String filePath, String fileName, String downloadWay)
throws Exception {
try {
response.addHeader("Content-Disposition", downloadWay + "; filename=" + fileName);
InputStream is = new FileInputStream(filePath);
byte[] b = new byte[8 * 1024];
int length = -1;
OutputStream out = response.getOutputStream();
while ((length = is.read(b)) != -1) {
out.write(b, 0, length);// 输出
}
out.close();
is.close();
} catch (Exception e) {
if (logger.isErrorEnabled()) {
logger.error(ExceptionUtils.getMessage(e));
}
throw e;
}
}
}
经过测试,没有任何问题:
浏览器输入下载地址:
http://localhost:8088/api/DownloadResourceFile/downloadFile