前台页面请求:
window.location.href = "/download/download?fileUrl=" + data.contractUrl + "&originName=" + data.originName;
后台下载处理:
import com.alibaba.dubbo.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
/**
* 文件下载共通实现
*
*/
@Controller
@RequestMapping("/download")
public class DownloadController extends BaseController {
/**
* 日志
*/
private static final Logger LOGGER = LoggerFactory.getLogger(DownloadController.class);
/**
* 文件下载方法实现
*
* @param response
* @param fileUrl 待下载文件路径
* @param originName 待下载文件的原文件名
*/
@RequestMapping("/download")
public void download(HttpServletResponse response, String fileUrl, String originName) {
// 参数不能为空,否则不支持下载
if (StringUtils.isEmpty(fileUrl) || StringUtils.isEmpty(originName) || response == null) {
return;
}
fileUrl = Constants.WEB_IMAGE_URL + fileUrl;
// 执行下载
ServletOutputStream sos = null;
BufferedInputStream bis = null;
InputStream inputStream = null;
HttpURLConnection httpUrlConn = null;
try {
LOGGER.info("执行下载开始:fileUrl=" + fileUrl + ";originName=" + originName);
// 清空response
response.reset();
// 设置响应头
response.setContentType("application/x-msdownload");
// 重命名为原文件名称
// 解决中文名称乱码
originName = new String(originName.getBytes("gbk"), "iso-8859-1");
response.addHeader("Content-Disposition", "attachment; filename=" + originName);
// 获取输出流
sos = response.getOutputStream();
// 获取网络路径连接
URL url = new URL(fileUrl);
httpUrlConn = (HttpURLConnection) url.openConnection();
// 设置输入开启
httpUrlConn.setDoInput(true);
// 显性设置缓存
httpUrlConn.setUseCaches(true);
httpUrlConn.setRequestMethod("GET");
// 设置网络请求头
httpUrlConn.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
httpUrlConn.connect();
// 通过连接获取输入流
inputStream = httpUrlConn.getInputStream();
bis = new BufferedInputStream(httpUrlConn.getInputStream());
int i;
// 设置下载缓冲
/*byte[] buffer = new byte[1024];*/
while ((i = bis.read()) != -1) {
sos.write(i);
}
sos.flush();
LOGGER.info("执行下载正常结束!");
} catch (UnsupportedEncodingException e) {
printLog(fileUrl, originName);
e.printStackTrace();
} catch (ProtocolException e) {
printLog(fileUrl, originName);
e.printStackTrace();
} catch (MalformedURLException e) {
printLog(fileUrl, originName);
e.printStackTrace();
} catch (IOException e) {
printLog(fileUrl, originName);
e.printStackTrace();
// 关闭流
} finally {
closeStream(sos, bis, inputStream, httpUrlConn);
}
}
/**
* 输出日志
*
* @param fileUrl
* @param originName
*/
private void printLog(String fileUrl, String originName) {
LOGGER.info("执行下载失败:fileUrl=" + fileUrl + ";originName=" + originName);
}
/**
* 关闭流
*
* @param sos
* @param bis
* @param inputStream
* @param httpUrlConn
*/
private void closeStream(ServletOutputStream sos, BufferedInputStream bis
, InputStream inputStream, HttpURLConnection httpUrlConn) {
try {
// 释放资源
if (sos != null) {
sos.close();
}
if (bis != null) {
bis.close();
}
if (inputStream != null) {
inputStream.close();
}
// 断开连接
if (httpUrlConn != null) {
httpUrlConn.disconnect();
}
} catch (IOException ex) {
LOGGER.info("断开连接出错!");
ex.printStackTrace();
}
}
}