package com.caiex.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Component;
import com.caiex.exception.DownloadException;
import com.caiex.global.Constants;
/**
* 文件下载工具类
*
* @author wsy
*
*/
@Component
public class FileOperateUtil {
/**
*
* @param response
* 响应对象 ,用于获取输出流
* @param fileName
* 文件名
* @throws DownloadException
* 文件不存在或者io异常时抛出此异常
*/
public static void download(HttpServletResponse response, String fileName) throws DownloadException {
String downLoadPath = Constants.XLS_DOWNLOAD_PATH + fileName;
long fileLength = new File(downLoadPath).length();
response.setContentType("application/octet-stream");
try {
response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1"));
} catch (UnsupportedEncodingException e) {
}
response.setHeader("Content-Length", String.valueOf(fileLength));
FileInputStream fis = null;
ServletOutputStream os = null;
try {
fis = new FileInputStream(downLoadPath);
os = response.getOutputStream();
IOUtils.write(IOUtils.toByteArray(fis), os);
} catch (FileNotFoundException e) {
throw new DownloadException("文件未找到");
} catch (IOException e) {
throw new DownloadException("io异常");
} finally {
IOUtils.closeQuietly(fis);
IOUtils.closeQuietly(os);
}
}
}
文件下载工具类
最新推荐文章于 2024-08-02 14:02:03 发布