Survey/src/main/java/com/example/survey/util/DownloadUtil.java
2021-05-23 14:57:31 +08:00

28 lines
880 B
Java

package com.example.survey.util;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;
public class DownloadUtil {
public static void downloadFile(String path, HttpServletResponse response) throws IOException {
response.addHeader("Content-Disposition", "attachment;filename=" + UUID.randomUUID().toString() + ".docx");
FileInputStream inputStream = new FileInputStream(path);
int count =0;
byte[] by = new byte[1024];
OutputStream out = response.getOutputStream();
while((count=inputStream.read(by))!=-1){
out.write(by, 0, count);//将缓冲区的数据输出到浏览器
}
inputStream.close();
out.flush();
out.close();
}
}