zoukankan      html  css  js  c++  java
  • 下载远程文件

    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;

      

    /** * 下载飞行资料夹 * * @param fileName * @param response * @return */ @RequestMapping(value = "/download", method = {RequestMethod.POST, RequestMethod.GET}) @ResponseBody public Object onDownload(String fileName, HttpServletResponse response) { ResponseJson json = new ResponseJson(); HttpURLConnection con = null; InputStream is = null; try { URL url = new URL(ServiceConstants.DOWNLOAD_URL_FLIGHT + fileName); // 远程文件url,如:http://211.64.201.201/uploadfile/nyz.mp3 con = (HttpURLConnection) url.openConnection(); response.setContentType("multipart/form-data"); // 自动识别下载文件格式 response.setHeader("Content-Disposition", "attachment;fileName=" + fileName.substring(8, fileName.length())); // 文件名 response.setHeader("Content-Length", con.getHeaderField("Content-Length")); // 文件大小 is = con.getInputStream(); byte[] b = new byte[1024]; int len = 0; while ((len = is.read(b)) != -1) { response.getOutputStream().write(b, 0, len); // 仅写入读取到的字符,区别于write(b) } response.getOutputStream().flush(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (con != null) { con.disconnect(); } if (is != null) { is.close(); } if (response.getOutputStream() != null) { response.getOutputStream().close(); } } catch (IOException e) { e.printStackTrace(); } } json.setSuccess(true); return json; }
  • 相关阅读:
    HDU 3507 PrintArticle (单调队列优化)
    BZOJ 1911 (特别行动队)
    POJ 3709 K-Anonymous Sequence (单调队列优化)
    邓_php面试【002】——完整版
    邓_正则表达式
    邓_PHP面试2
    邓_PHP面试【001】
    网站大全
    Jquery 获取对象的几种方式介绍
    邓_Jquery测试题
  • 原文地址:https://www.cnblogs.com/zhiqsyr/p/3921515.html
Copyright © 2011-2022 走看看