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
| public class DownloadUtil { public boolean download(String path, String name, HttpServletRequest request, HttpServletResponse response){
FileInputStream fileIn = null; ServletOutputStream out = null; try { response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(name+".PNG", "UTF-8")); File file; String filePathString = path + name+".PNG"; file = new File(filePathString); fileIn = new FileInputStream(file); out = response.getOutputStream(); byte[] outputByte = new byte[1024]; int readTmp = 0; while ((readTmp = fileIn.read(outputByte)) != -1) { out.write(outputByte, 0, readTmp); } } catch (Exception e) { e.printStackTrace(); } finally { try { fileIn.close(); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } } return true; } }
|