HTTP achieve

HTTP implementation:

In simple terms it should be divided into two angles to look at:

1, will resume file

2, the resume file

First, the file will resume

Let us talk about going to resume the file, since it is a resume, then surely there must be a logo, an example to illustrate:

    Prepared a document, the size is 1852 bytes, the first reading of the file io stream (0-1024 bytes), and 1024 as the first value of the next read, this variable is stored 1024, it is in the process the identification (record read position), through this identity, we next read byte range should be the (1024-1852) bytes.

   To accomplish this requirement, we should consider two angles,

   1), the logo put there (after I held it Liezi is on persistence the Map [ ConcurrentHashMap ])

   2), since it is the resume, then certainly a need to identify the end of the state, to tell resume file, I have run over.

   Here is the key code:

    (1),ConcurrentHashMap

     ConcurrentHashMap advantages:

    ConcurrentHashMap allowed while updating, while traversing, that is to say in Iterator object traversal time,

    ConcurrentHashMap data can also remove, put the operation, and the data will traverse as remove, put operational changes in output, that is to say when the object Iterator traversal, ConcurrentHashMap also can remove, put the operation, and will traverse with remove, put operational changes in output.

    In short: it uses only the synchronous operation of the put, remove operation, get the operation does not affect

    code show as below:

package ga.gaapi.config;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ZjHashMap {
    static Map<String,Long> conMap = new ConcurrentHashMap<>();
    //新增
    public static void addMap(String key,Long value){
        conMap.put(key,value);
    }
    //删除
    public  static void delect(String key){
        conMap.remove(key);
    }

    //获取
    public static  Long get(String key){
        return  conMap.get(key);
    }

}

(2), part of the core code reads the file:

path: the path,

fileName: file name.

try{    

    File file = new File(path+"\\"+fileName);
    RandomAccessFile raf = new RandomAccessFile(file,"r");
    long count = file.length();
    byte[] read = new byte[Integer.parseInt(size)];
    int len =0;
    long fy = 0;
    long flag = 1;

     //判断标识中是否有值,有值的话,作为读取的首值
     if (null!= ZjHashMap.get(fileName)){
        raf.seek(ZjHashMap.get(fileName));
        //计算要分页的数量,用来判断是否已经全部读取
        fy = PostUtils.fy(count-ZjHashMap.get(fileName), Long.parseLong(size));
     }else{
        //计算要分页的数量,用来判断是否已经全部读取
        fy = PostUtils.fy(count, Long.parseLong(size));
     }
          while((len=raf.read(read))!=-1){
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            outputStream.write(read,0,len);
            outputStream.flush();
            //读取的字节
            byte[] bytes = outputStream.toByteArray();

            //把字节数发送给续传文件方

            //当前字节数目
            ZjHashMap.addMap(fileName,raf.getFilePointer());
              if (flag == fy){
                 //表示已到结尾,可以将这部分逻辑发送给续传文件方,告诉它已续传完毕
              }
            flag ++;
            outputStream.close();
         }
     raf.close();
     ZjHashMap.delect(fileName);

}catch (Exception e){

           
}

Two , the resume file

Resume document like that, and for as long as a resume position identifier, and a corresponding byte stream can be a code as follows:

filePath: generated files, resume use for

content: bytes to be written

position: resume byte positions

 RandomAccessFile raf = null;
        try {
            raf = new RandomAccessFile(filePath, "rw"); //将随机存取文件流连接到文件,访问方式设置为可读可写
            raf.seek(position); //指定插入的位置
            raf.write(content); //插入指定内容
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭随机存取文件流
            try {
                raf.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

 

Published 95 original articles · won praise 180 · views 40000 +

Guess you like

Origin blog.csdn.net/weixin_38316697/article/details/93502897