import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
public class DownFile {
/**
* filename 下载到客户端后的文件名称
* filepath 文件的路径 绝对路径和相对路径都可以
*下载 文件
*/
public static void downZip(String filename,String filepath,HttpServletResponse response) {
FileInputStream inStream=null;
try {
File zip=new File(filepath);// 文件
inStream=new FileInputStream(zip);
byte[] buf=new byte[4096];
int readLength;
setResponseHeader(response, filename);
while (((readLength=inStream.read(buf)) != -1)) {
response.getOutputStream().write(buf, 0, readLength);
}
}catch (Exception e){
try {
OutputStream outputStream=response.getOutputStream();//获取OutputStream输出流
response.setHeader("c
java通过HttpServletResponse下载文件到客户端
最新推荐文章于 2024-07-29 02:51:40 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)