上傳下載工具類
阿新 • • 發佈:2019-01-02
1、下載
public static void downloadFile(HttpServletResponse response, String filepath, String fileName) throws IOException { try { File file = new File(path.concat(filepath)); if (StringUtils.isBlank(fileName)) { fileName = file.getName(); } response.addHeader("Content-Disposition","attachment; filename="+ new String(fileName.getBytes("GB2312"), "ISO-8859-1")); response.setContentType("application/x-msdownload"); response.setContentLength((int) file.length()); InputStream inputStream = new FileInputStream(file); OutputStream os = response.getOutputStream(); byte[] b = new byte[1024]; int length; while ((length = inputStream.read(b)) > 0) { os.write(b, 0, length); } inputStream.close(); os.close(); } catch (FileNotFoundException e) { log.error("檔案找不到", e); } catch (IOException e) { log.error("io異常", e); } }