Network multiline thread download and merge file example, multiline thread merge example

Source: Internet
Author: User

Network multiline thread download and merge file example, multiline thread merge example

In this example, I/O streams and multiple threads are used to download the same image. After the download is completed, the thread creation method is used to control the execution time of the main thread, that is, the method called by the main thread, create a thread in the method, and then return the object that calls the method. Then execute other programs to effectively prevent other methods of the main thread from being executed when the download thread (that is, other threads) is not completed, however, data is missing due to misplacement.

1 import java. io. *; 2 import java.net. URL; 3 import java.net. URLConnection; 4 5/** 6 * Created by gdkong on 07/11/2017. 7 */8 9 public class Test {10 11 public static void main (String [] args) throws IOException, InterruptedException {12 // The main thread uses the call method mode to create other threads, other methods in the main line can be executed only after other threads are executed. This method can control the execution time of the Main Line 13 multithreadedDownload ("http: // localhost: 8080/image/a.jpg ", "/Users/Shared/abc", 3); 14} 15 16/** 1 7 * multi-threaded download and merge files, applicable only to Http protocol, no other network protocol is attempted 18 * @ param srcPath download source file path 19 * @ param destPath save folder path 20 * @ param count specified thread count 21 */22 public static void multithreadedDownload (String srcPath, string destPath, int count) throws IOException, InterruptedException {23 24 // defines the folder path for downloading an object, 25 File dir = new File (destPath); 26 if (! Dir. exists () {27 dir. mkdirs (); 28} 29 long total = getFileLength (srcPath); 30 // enable download thread 31 OpenMultithreadedDownload (srcPath, dir, total, count ); 32 33 // get the file suffix 34 String suffix = srcPath. substring (srcPath. lastIndexOf (". "); 35 // merge file 36 FileUtils. merge (dir, suffix); 37 38 System. out. println ("file download completed "); 39} 40 41/** 42 * multi-thread download block 43 * @ param path download source file path 44 * @ param dir target file storage directory 45 * @ p Aram total download source File size 46 * @ param count specifies the number of threads 47 */48 private static void OpenMultithreadedDownload (String path, File dir, long total, int count) throws InterruptedException {49 50 // calculate the average number of current bytes of each thread 51 long size = total/count; 52 // use a loop, calculate the start and end positions of each thread download 53 for (int I = 1; I <= count; I ++) {54 // start position 55 long starIndex = (I-1) * size; 56 // end position 57 long endIndex = I * size-1; 58 // The last thread 59 if (I = 3) {60 endIndex = total-1; 61} 62 DownloadThread dt = new DownloadThread (path, dir, starIndex, endIndex, I); 63 dt. start (); 64} 65} 66 67/** 68 * Get download source file size 69 * @ param path download source file path 70 **/71 public static long getFileLength (String path) throws IOException {72 73 URL url = new URL (path); 74 // get the connection object 75 URLConnection con = url. openConnection (); 76 long total = con. getContentLeng ThLong (); 77 return total; 78} 79/** 80 * merge file function 81 * @ param dir the parent directory of the source file storage 82 * @ param suffix file suffix name, used to filter files 83 */84 public static void merge (File dir, String suffix) throws IOException {85 86 File [] files = dir. listFiles (FileUtils. getFileFilter (suffix, false); 87 FileOutputStream fos = getFileOutputStream (dir, files [0]); 88 89 for (File file: files) {90 FileInputStream FCM = new FileInputSt Ream (file); 91 int len; 92 byte [] buf = new byte [8192]; 93 while (len = Fi. read (buf ))! =-1) {94 fos. write (buf, 0, len); 95} 96 fiis. close (); 97 // Delete 98 files from merged files. delete (); 99} 100 fos. close (); 101} 102 // get the output stream of the merged File 103 private static FileOutputStream getFileOutputStream (File dir, file File) throws FileNotFoundException {104 file file1 = File; 105 String name = file1.getName (); 106 name = name. substring (1); 107 File destFile = new File (dir, name); 108 return new FileOutputStream (de StFile); 109} 110/** 111 * file filter 112 * @ param suffix file suffix 113 * @ param containDirectory whether to display subfolders. true indicates display, false does not display 114 */115 public static FileFilter getFileFilter (String suffix, boolean containDirectory) {116 return new FileFilter () {117 @ Override118 public boolean accept (File file) {119 String name = file. getName (); 120 boolean destFormat = file. isFile () & name. endsWith (suffix); 121 boolean B; 122 1 23 B = containDirectory & file. isDirectory (); 124 125 if (destFormat | B) return true; 126 else return false; 127} 128 }; 129} 130} 131 132 // download Thread code 133 class DownloadThread extends Thread {134 135 private final String path; 136 private final File dir; 137 private final long startIndex; 138 private final long endIndex; 139 private final int threadID; 140 // use the constructor to input the 141 public DownloadThread (String path, File dir, long startIndex, 142 long endIndex, int threadID) {143 this. path = path; 144 this. dir = dir; 145 this. startIndex = startIndex; 146 this. endIndex = endIndex; 147 this. threadID = threadID; 148} 149 @ Override150 public void run () {151 try {152 InputStream in = getInputStream (); 153 FileOutputStream fos = getFileOutputStream (); 154 int len; 155 byte [] arr = new byte [8192]; 156 while (len = in. rea D (arr ))! =-1) {157 fos. write (arr, 0, len); 158} 159 in. close (); 160 fos. close (); 161} catch (Exception e) {162 e. printStackTrace (); 163} 164 165} 166 // get download output stream 167 private FileOutputStream getFileOutputStream () throws FileNotFoundException {168 // get the file name 169 int index = path. lastIndexOf ("/") + 1; 170 String name = path. substring (index); 171 File file = new File (dir, threadID + name); // create the target File key, prevent files from overwriting 172 // create an output stream 173 return new FileOutputStream (file); 174} 175 // obtain the downloaded input stream 176 private InputStream getInputStream () throws IOException {177 // create a URL object 178 URL url = new URL (path); 179 // obtain the connection object 180 URLConnection con = url. openConnection (); 181 con. setRequestProperty ("Range", "bytes =" + startIndex + "-" + endIndex); 182 // get the input stream 183 return con. getInputStream (); 184} 185}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.