中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Java 下載支持?jǐn)帱c(diǎn)續(xù)傳服務(wù)端

2018-07-20    來源:open-open

容器云強(qiáng)勢上線!快速搭建集群,上萬Linux鏡像隨意使用
 
private void downFile(HttpServletResponse response, HttpServletRequest request, String location){
    BufferedInputStream bis = null;
    try {
        File file = new File(location);
        if (file.exists()) {
            long p = 0L;
            long toLength = 0L;
            long contentLength = 0L;
            int rangeSwitch = 0; // 0,從頭開始的全文下載;1,從某字節(jié)開始的下載(bytes=27000-);2,從某字節(jié)開始到某字節(jié)結(jié)束的下載(bytes=27000-39000)
            long fileLength;
            String rangBytes = "";
            fileLength = file.length();

            // get file content
            InputStream ins = new FileInputStream(file);
            bis = new BufferedInputStream(ins);

            // tell the client to allow accept-ranges
            response.reset();
            response.setHeader("Accept-Ranges", "bytes");

            // client requests a file block download start byte
            String range = request.getHeader("Range");
            if (range != null && range.trim().length() > 0 && !"null".equals(range)) {
                response.setStatus(javax.servlet.http.HttpServletResponse.SC_PARTIAL_CONTENT);
                rangBytes = range.replaceAll("bytes=", "");
                if (rangBytes.endsWith("-")) {  // bytes=270000-
                    rangeSwitch = 1;
                    p = Long.parseLong(rangBytes.substring(0, rangBytes.indexOf("-")));
                    contentLength = fileLength - p;  // 客戶端請求的是270000之后的字節(jié)(包括bytes下標(biāo)索引為270000的字節(jié))
                } else { // bytes=270000-320000
                    rangeSwitch = 2;
                    String temp1 = rangBytes.substring(0, rangBytes.indexOf("-"));
                    String temp2 = rangBytes.substring(rangBytes.indexOf("-") + 1, rangBytes.length());
                    p = Long.parseLong(temp1);
                    toLength = Long.parseLong(temp2);
                    contentLength = toLength - p + 1; // 客戶端請求的是 270000-320000 之間的字節(jié)
                }
            } else {
                contentLength = fileLength;
            }

            // 如果設(shè)設(shè)置了Content-Length,則客戶端會(huì)自動(dòng)進(jìn)行多線程下載。如果不希望支持多線程,則不要設(shè)置這個(gè)參數(shù)。
            // Content-Length: [文件的總大小] - [客戶端請求的下載的文件塊的開始字節(jié)]
            response.setHeader("Content-Length", new Long(contentLength).toString());

            // 斷點(diǎn)開始
            // 響應(yīng)的格式是:
            // Content-Range: bytes [文件塊的開始字節(jié)]-[文件的總大小 - 1]/[文件的總大小]
            if (rangeSwitch == 1) {
                String contentRange = new StringBuffer("bytes ").append(new Long(p).toString()).append("-")
                        .append(new Long(fileLength - 1).toString()).append("/")
                        .append(new Long(fileLength).toString()).toString();
                response.setHeader("Content-Range", contentRange);
                bis.skip(p);
            } else if (rangeSwitch == 2) {
                String contentRange = range.replace("=", " ") + "/" + new Long(fileLength).toString();
                response.setHeader("Content-Range", contentRange);
                bis.skip(p);
            } else {
                String contentRange = new StringBuffer("bytes ").append("0-")
                        .append(fileLength - 1).append("/")
                        .append(fileLength).toString();
                response.setHeader("Content-Range", contentRange);
            }

            String fileName = file.getName();
            response.setContentType("application/octet-stream");
            response.addHeader("Content-Disposition", "attachment;filename=" + fileName);

            OutputStream out = response.getOutputStream();
            int n = 0;
            long readLength = 0;
            int bsize = 1024;
            byte[] bytes = new byte[bsize];
            if (rangeSwitch == 2) {
                // 針對 bytes=27000-39000 的請求,從27000開始寫數(shù)據(jù)                    
                while (readLength <= contentLength - bsize) {
                    n = bis.read(bytes);
                    readLength += n;
                    out.write(bytes, 0, n);
                }
                if (readLength <= contentLength) {
                    n = bis.read(bytes, 0, (int) (contentLength - readLength));
                    out.write(bytes, 0, n);
                }                   
            } else {
                while ((n = bis.read(bytes)) != -1) {
                    out.write(bytes,0,n);                                                      
                }                   
            }
            out.flush();
            out.close();
            bis.close();
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug("Error: file " + location + " not found.");
            }                
        }
    } catch (IOException ie) {
        // 忽略 ClientAbortException 之類的異常
    } catch (Exception e) {
        logger.error(e);
    }
}
 

標(biāo)簽: isp

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:PHP根據(jù)圖片色界在不同位置加水印

下一篇:php來實(shí)現(xiàn)telnet的連接、傳遞命令、獲取返回值等功能!