URL下載檔案伺服器檔案
阿新 • • 發佈:2019-01-10
前臺頁面請求:
window.location.href = "/download/download?fileUrl=" + data.contractUrl + "&originName=" + data.originName;
後臺下載處理:
import com.alibaba.dubbo.common.utils.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.ProtocolException; import java.net.URL; /** * 檔案下載共通實現 * */ @Controller @RequestMapping("/download") public class DownloadController extends BaseController { /** * 日誌 */ private static final Logger LOGGER = LoggerFactory.getLogger(DownloadController.class); /** * 檔案下載方法實現 * * @param response * @param fileUrl 待下載檔案路徑 * @param originName 待下載檔案的原檔名 */ @RequestMapping("/download") public void download(HttpServletResponse response, String fileUrl, String originName) { // 引數不能為空,否則不支援下載 if (StringUtils.isEmpty(fileUrl) || StringUtils.isEmpty(originName) || response == null) { return; } fileUrl = Constants.WEB_IMAGE_URL + fileUrl; // 執行下載 ServletOutputStream sos = null; BufferedInputStream bis = null; InputStream inputStream = null; HttpURLConnection httpUrlConn = null; try { LOGGER.info("執行下載開始:fileUrl=" + fileUrl + ";originName=" + originName); // 清空response response.reset(); // 設定響應頭 response.setContentType("application/x-msdownload"); // 重新命名為原檔名稱 // 解決中文名稱亂碼 originName = new String(originName.getBytes("gbk"), "iso-8859-1"); response.addHeader("Content-Disposition", "attachment; filename=" + originName); // 獲取輸出流 sos = response.getOutputStream(); // 獲取網路路徑連線 URL url = new URL(fileUrl); httpUrlConn = (HttpURLConnection) url.openConnection(); // 設定輸入開啟 httpUrlConn.setDoInput(true); // 顯性設定快取 httpUrlConn.setUseCaches(true); httpUrlConn.setRequestMethod("GET"); // 設定網路請求頭 httpUrlConn.setRequestProperty("Content-type", "application/x-www-form-urlencoded"); httpUrlConn.connect(); // 通過連接獲取輸入流 inputStream = httpUrlConn.getInputStream(); bis = new BufferedInputStream(httpUrlConn.getInputStream()); int i; // 設定下載緩衝 /*byte[] buffer = new byte[1024];*/ while ((i = bis.read()) != -1) { sos.write(i); } sos.flush(); LOGGER.info("執行下載正常結束!"); } catch (UnsupportedEncodingException e) { printLog(fileUrl, originName); e.printStackTrace(); } catch (ProtocolException e) { printLog(fileUrl, originName); e.printStackTrace(); } catch (MalformedURLException e) { printLog(fileUrl, originName); e.printStackTrace(); } catch (IOException e) { printLog(fileUrl, originName); e.printStackTrace(); // 關閉流 } finally { closeStream(sos, bis, inputStream, httpUrlConn); } } /** * 輸出日誌 * * @param fileUrl * @param originName */ private void printLog(String fileUrl, String originName) { LOGGER.info("執行下載失敗:fileUrl=" + fileUrl + ";originName=" + originName); } /** * 關閉流 * * @param sos * @param bis * @param inputStream * @param httpUrlConn */ private void closeStream(ServletOutputStream sos, BufferedInputStream bis , InputStream inputStream, HttpURLConnection httpUrlConn) { try { // 釋放資源 if (sos != null) { sos.close(); } if (bis != null) { bis.close(); } if (inputStream != null) { inputStream.close(); } // 斷開連線 if (httpUrlConn != null) { httpUrlConn.disconnect(); } } catch (IOException ex) { LOGGER.info("斷開連接出錯!"); ex.printStackTrace(); } } }