Java tutorial
/** * Copyright (c) 2012-2014 http://www.eryansky.com * * Licensed under the Apache License, Version 2.0 (the "License"); */ package com.eryansky.common.web.utils; import com.eryansky.common.utils.StringUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.ArrayUtils; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; /** * * @author : &Eryan eryanwcp@gmail.com * @date : 2014-05-05 22:50 */ public class DownloadUtils { /** * * @param request * @param response * @param filePath * @throws IOException */ public static void download(HttpServletRequest request, HttpServletResponse response, String filePath) throws IOException { download(request, response, filePath, ""); } /** * * @param request * @param response * @param filePath * @param displayName ?? * @throws IOException */ public static void download(HttpServletRequest request, HttpServletResponse response, String filePath, String displayName) throws IOException { File file = new File(filePath); if (StringUtils.isEmpty(displayName)) { displayName = file.getName(); } if (!file.exists() || !file.canRead()) { response.setContentType("text/html;charset=utf-8"); response.getWriter().write("??"); return; } download(request, response, new FileInputStream(file), displayName); } /** * * @param request * @param response * @param displayName ?? * @param bytes * @throws IOException */ public static void download(HttpServletRequest request, HttpServletResponse response, byte[] bytes, String displayName) throws IOException { if (ArrayUtils.isEmpty(bytes)) { response.setContentType("text/html;charset=utf-8"); response.setCharacterEncoding("utf-8"); response.getWriter().write("??"); return; } download(request, response, new ByteArrayInputStream(bytes), displayName); } /** * * @param request * @param response * @param inputStream ? * @param displayName ?? * @throws IOException */ public static void download(HttpServletRequest request, HttpServletResponse response, InputStream inputStream, String displayName) throws IOException { response.reset(); WebUtils.setNoCacheHeader(response); response.setContentType("application/x-download"); response.setContentLength((int) inputStream.available()); // String displayFilename = displayName.substring(displayName.lastIndexOf("_") + 1); // displayFilename = displayFilename.replace(" ", "_"); WebUtils.setDownloadableHeader(request, response, displayName); BufferedInputStream is = null; OutputStream os = null; try { os = response.getOutputStream(); is = new BufferedInputStream(inputStream); IOUtils.copy(is, os); } catch (Exception e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(is); } } }