当点击下载的时候文件,不是我们平时见到的那种,弹出下载提示框,选择保存位置,而是直接跳转到新的页面,页面显示的是文件的乱码。

URL:reportMainDodyUrl【例如:http://down10.zol.com.cn/skycndownernew/keaibizhi83_346188.exe】

求大神看一下!

private void downLoadNet(String reportMainDodyUrl) throws IOException {

        int last = reportMainDodyUrl.lastIndexOf("/");

        String filename = reportMainDodyUrl.substring(last+1, reportMainDodyUrl.length());

        

        URL url = new URL(reportMainDodyUrl);

        InputStream inStream = null;

        ServletOutputStream ouputStream = null;

        try {

            URLConnection conn = url.openConnection();

            inStream = conn.getInputStream();

           

            response.reset();

            response.setContentType("application/octet-stream");

            response.addHeader("Content-Disposition", "attachment;filename=" + filename);

            ouputStream = response.getOutputStream();

            

            byte[] buffer = new byte[1204];

            int byteread = 0;

            while ((byteread = inStream.read(buffer)) != -1) {

            ouputStream.flush();

                ouputStream.write(buffer, 0, byteread);

            }

        } catch (Exception e) {

        logger.error("File download failed!", e);

        } finally {

        if (inStream != null){

        inStream.close();

        }

        if (ouputStream != null) {

        ouputStream.flush();

        ouputStream.close();

        }

        }

    }


NeverGiveUp7
浏览 1985回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java