java 服务器 http下载转发server

HttpURLConnection 文件下载转发细节

细节:outputStream对象必须在inputStream对象之前创建,否则会引起bug(即用户第一次点击发起下载请求会一直等待, 必须再次点击才能下载)
使用的是springMVC,下载文件的action如下

@RequestMapping("/report_generalPdf")
    public void downloadFile(@RequestParam("filePath") String filePath, HttpServletRequest request, HttpServletResponse response) {
        if (filePath != null) {
            response.addHeader("Content-Disposition",
                    "attachment;fileName=" + "result.pdf");// 设置文件名
            byte[] buffer = new byte[1024];
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            try {
                // 测试用的内网资源
                URL url = new URL("http://192.168.xxx.xx/uploadFiles/report/xxx/xxx/xxx_report.pdf");
                // 连接类的父类,抽象类
                URLConnection urlConnection = url.openConnection();
                // http的连接类
                HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
                // 设定请求的方法,默认是GET
                httpURLConnection.setRequestMethod("GET");
                // 设置字符编码
                httpURLConnection.setRequestProperty("Charset", "UTF-8");
                // 打开到此 URL 引用的资源的通信链接(如果尚未建立这样的连接)。
                httpURLConnection.connect();
                httpURLConnection.setConnectTimeout(100);
                httpURLConnection.setReadTimeout(100);
                // 文件大小
//              int fileLength = httpURLConnection.getContentLength();

                // 文件名
//              String filePathUrl = httpURLConnection.getURL().getFile();
//              String fileFullName = filePathUrl.substring(filePathUrl.lastIndexOf(File.separatorChar) + 1);

//              System.out.println("file length---->" + fileLength);
                OutputStream os = response.getOutputStream();
                int i = bis.read(buffer);
                bis = new BufferedInputStream(httpURLConnection.getInputStream());
                while (i != -1) {
                    os.write(buffer, 0, i);
                    i = bis.read(buffer);
                }


            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            } finally {
                if (bis != null) {
                    try {
                        bis.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }

        }
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,259评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 33,719评论 18 399
  • 1. 网络基础TCP/IP HTTP基于TCP/IP协议族,HTTP属于它内部的一个子集。 把互联网相关联的协议集...
    yozosann阅读 8,823评论 0 20
  • 怎样才算介入了你的生活。
    是唯一一切阅读 1,229评论 0 0
  • 儿子这几天学校了在学唱电影《狮子王》主题曲:Can you feel the love tonight .要我帮着...
    有趣的谷粒阅读 3,843评论 0 0

友情链接更多精彩内容