免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Java 下載支持斷點續(xù)傳

[Java]代碼,服務器端實現

01File file = new File(location);                        
02                        if (file.exists()) {                                        
03                            long p = 0;
04                            long fileLength;
05                            fileLength = file.length();
06                              
07                            // get file content
08                            InputStream ins = new FileInputStream(file);
09                            bis = new BufferedInputStream(ins);                            
10                              
11                            // tell the client to allow accept-ranges
12                            response.reset();
13                            response.setHeader("Accept-Ranges", "bytes");
14                              
15                            // client requests a file block download start byte
16                            if (request.getHeader("Range") != null) {                                
17                                response.setStatus(javax.servlet.http.HttpServletResponse.SC_PARTIAL_CONTENT);
18                                p = Long.parseLong(request.getHeader("Range")
19                                        .replaceAll("bytes=", "")
20                                        .replaceAll("-", "")
21                                        );                                
22                            }
23                            // support multi-threaded download
24                            // respone format:
25                            // Content-Length:[file size] - [client request start bytes from file block]
26                            response.setHeader("Content-Length", new Long(fileLength - p).toString());
27                              
28                            if (p != 0) {
29                                // 斷點開始
30                                // 響應的格式是:
31                                // Content-Range: bytes [文件塊的開始字節(jié)]-[文件的總大小 - 1]/[文件的總大小]
32                                String contentRange = new StringBuffer("bytes ")
33                                        .append(new Long(p).toString())
34                                        .append("-")
35                                        .append(new Long(fileLength - 1).toString())
36                                        .append("/")
37                                        .append(new Long(fileLength).toString())
38                                        .toString();
39                                response.setHeader("Content-Range", contentRange);
40                                // pointer move to seek
41                                bis.skip(p);
42                            }
43                              
44                            String fileName = file.getName();
45                            response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
46                                           
47                            while ((size = bis.read(buf)) != -1) {
48                                response.getOutputStream().write(buf,0,size);
49                                response.getOutputStream().flush();                                
50                            }
51                            bis.close();

[代碼] 客戶端下載測試

 
01public class TestDownload {
02  
03    /**
04     * @param args
05     */
06    public static void main(String[] args) {
07        // TODO Auto-generated method stub
08        HttpURLConnection httpURLConnection = null;
09        URL url = null;
10        BufferedInputStream bis = null;
11        byte[] buf = new byte[10240];
12        int size = 0;
13        String fileName = "aaa.zip";
14        String filePath = "C:\\Users\\Desktop";
15        String remoteUrl = "http://127.0.0.1:8080/down.zip";
16  
17        // 檢查本地文件
18        RandomAccessFile rndFile = null;
19        File file = new File(filePath + "\\" + fileName);
20        long remoteFileSize = getRemoteFileSzie(remoteUrl);
21        long nPos = 0;
22         
23        if (file.exists()) {                       
24            long localFileSzie = file.length();
25            if (localFileSzie < remoteFileSize) {                   
26                System.out.println("文件續(xù)傳...");
27                nPos = localFileSzie;
28            } else {
29                System.out.println("文件存在,重新下載...");
30                file.delete();
31                try {
32                    file.createNewFile();
33                } catch (Exception e) {
34                    // TODO: handle exception
35                    e.printStackTrace();
36                }   
37            }
38             
39        } else {
40            // 建立文件
41            try {
42                file.createNewFile();
43            } catch (Exception e) {
44                // TODO: handle exception
45                e.printStackTrace();
46            }           
47        }
48         
49        // 下載文件
50        try {
51            url = new URL(remoteUrl);       
52            httpURLConnection = (HttpURLConnection)url.openConnection();
53            // 設置User-Agent
54            httpURLConnection.setRequestProperty("User-Agent", "Net");
55            // 設置續(xù)傳開始
56            httpURLConnection.setRequestProperty("Range", "bytes=" + nPos + "-");
57            // 獲取輸入流
58            bis = new BufferedInputStream(httpURLConnection.getInputStream());           
59            rndFile = new RandomAccessFile(filePath + "\\" + fileName, "rw");
60            rndFile.seek(nPos);
61            int i = 0;
62            while ((size = bis.read(buf)) != -1) {
63                //if (i > 500) break;               
64                rndFile.write(buf, 0, size);
65                 
66                i++;
67            }
68            System.out.println("i=" + i);
69            httpURLConnection.disconnect();
70        } catch (Exception e) {
71            // TODO: handle exception
72            e.printStackTrace();
73        }
74    }
75  
76    public static long getRemoteFileSzie(String url) {
77        long size = 0;
78        try {
79            HttpURLConnection httpUrl = (HttpURLConnection)(new URL(url)).openConnection();
80            size = httpUrl.getContentLength();
81            httpUrl.disconnect();           
82        } catch (Exception e) {
83            // TODO: handle exception
84            e.printStackTrace();
85        }
86        return size;
87    }
88}
本站僅提供存儲服務,所有內容均由用戶發(fā)布,如發(fā)現有害或侵權內容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
使用HttpClient實現文件的上傳下載
ASP.NET上傳文件的幾種方法
Java 下載支持斷點續(xù)傳服務端
Java實現的斷點續(xù)傳功能
ASP.NET上傳和下載文件的代碼(轉載)
Java利用HttpURLConnection發(fā)送post請求上傳文件
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯系客服!

聯系客服