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; }
  • 相关阅读:
    ABAP ole方式对EXCEL进行操作
    ABAP 通过视图取数到内表函数
    ABAP 数值类型转换
    ABAP 供应商、工厂对应公里数维护
    deb包转换为rpm包格式
    Linux统计即时网速
    RedHat可用的几处软件源
    linux 技巧:使用 screen 管理你的远程会话
    国内常用Linux镜像站点
    telnet访问出现telnet:Unable to connect to remote host: No route to host
  • 原文地址:https://www.cnblogs.com/zhiqsyr/p/3921515.html
Copyright © 2011-2022 走看看