1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| import java.io.File; import javax.servlet.http.HttpServletResponse;
public class FileUtil {
public static void fileDownload(HttpServletResponse response, File file) {
FileInputStream fileInputStream = null;
ServletOutputStream outputStream = null;
try {
fileInputStream = new FileInputStream(file);
response.setHeader("content-disposition", "attachment;fileName=" + URLEncoder .encode(file.getName(), CharsetUtil.UTF_8));
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
outputStream = response.getOutputStream(); IOUtils.copy(fileInputStream, outputStream);
} catch (Exception e) { throw new RuntimeException(e);
} finally { try { if (outputStream != null) outputStream.close(); } catch (Exception e) { e.printStackTrace(); } try { if (fileInputStream != null) fileInputStream.close(); } catch (Exception e) { e.printStackTrace(); } } } }
|