Java tutorial
/* * Copyright 2010-2011 ESunny.com All right reserved. This software is the confidential and proprietary information of * ESunny.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only * in accordance with the terms of the license agreement you entered into with ESunny.com. */ package com.usefullc.platform.common.utils; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.OutputStream; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang.StringUtils; /** * FileIoUtils.java??TODO ?? * * @author shengshang.tang 2014328 ?11:31:06 */ public class IOUtils { /** * * * @param path * @param fileName * @param response * @return */ public static void download(String path, String fileName, HttpServletResponse response) { try { if (StringUtils.isEmpty(path)) { throw new IllegalArgumentException("?"); } else { File file = new File(path); if (!file.exists()) { throw new IllegalArgumentException("??"); } } if (StringUtils.isEmpty(fileName)) { throw new IllegalArgumentException("???"); } if (response == null) { throw new IllegalArgumentException("response ?"); } // path File file = new File(path); // ?? InputStream fis = new BufferedInputStream(new FileInputStream(path)); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); // response response.reset(); // ??linux ? linux utf-8,windows GBK) String defaultEncoding = System.getProperty("file.encoding"); if (defaultEncoding != null && defaultEncoding.equals("UTF-8")) { response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("GBK"), "iso-8859-1")); } else { response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(), "iso-8859-1")); } // responseHeader response.addHeader("Content-Length", "" + file.length()); response.setContentType("application/octet-stream"); OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); toClient.write(buffer); toClient.flush(); toClient.close(); } catch (Exception ex) { ex.printStackTrace(); } } /** * * * @param content * @param fileName * @param response */ public static void download(byte[] content, String fileName, HttpServletResponse response) { try { // response response.reset(); // ??linux ? linux utf-8,windows GBK) String defaultEncoding = System.getProperty("file.encoding"); if (defaultEncoding != null && defaultEncoding.equals("UTF-8")) { response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("GBK"), "iso-8859-1")); } else { response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(), "iso-8859-1")); } // responseHeader response.addHeader("Content-Length", "" + content.length); response.setContentType("application/octet-stream"); OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); toClient.write(content); toClient.flush(); toClient.close(); } catch (Exception ex) { ex.printStackTrace(); } } }