Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Based on a complete solution RMI service to transfer large files     - rpm package specify the installation path (Linux)

- Ubuntu 14.04 Docker installation (Linux)

- Java how to achieve bubble sort the problem Arraylist (Programming)

- Port Telnet command to detect the remote host is turned on (Linux)

- Linux Mint brightness adjustment --xrandr command learning (Linux)

- Swift string common method (Programming)

- Linux instructions and examples GPG encryption and decryption (Linux)

- Linux tmux tcpdump summary (Linux)

- MySQL5.7 implement virtual column expression index (Database)

- jdbc Oracle database connection string writing pluggable (Database)

- Python-- for anomalies and reflection of objects articles (Programming)

- Vagrant failed to start, stuck in Waiting for VM to boot solution (Linux)

- Get the Linux device PCI ID method (Linux)

- On the design of Oracle database backup (Database)

- 10 important Linux ps command combat (Linux)

- Java 8 perspective annotation types (Programming)

- How to use the Linux kill command to kill the process / program is not responding (Linux)

- Oracle Linux 6.4 installed Oracle 11gR2 + RAC + ASM (Database)

- Restore Oracle Database Cold backup and database reconstruction emca (Database)

- Three binary tree traversal (recursive, non-recursive traversal and Morris) (Programming)

 
         
  Based on a complete solution RMI service to transfer large files
     
  Add Date : 2018-11-21      
         
         
         
  RMI-based service to transfer large files, uploading and downloading divided into two operations, we need to pay attention to the technical point there are three main aspects, the first data transmission services RMI must be serializable. Second, during the transfer of large files should schedule reminder system for large file transfers, this is very important, because a large file transfer time period often relatively long, must inform the real-time transmission of large files of user progress. Third, the read mode for large files, if one-time large file read into byte [] is unrealistic, because it will be subject to the JVM available memory, cause memory overflow problems.

Experiment RMI-based solutions and services to transfer large files, mainly focus on these three aspects gradual settlement. Will be uploading large files and downloading large files were elaborated.

1. upload large files based RMI service

1.1 Design Ideas

Upload large files divided into two stages, namely "CS handshake phase" and "upload file content update phase."

CS handshake phase, but also called basic information exchange phase between the client and the server. To determine the client local file to be uploaded and want to upload to the target path of the server. Read local files, access to the file length, the length of the local file, local file path and the target path of the server and other information passed to the server via the interface methods, then generate FileKey unique information from the server, and returned to the client, this FileKey as between the client and server that uniquely identifies the data transmission channel. These are the types of information and data Java basic data types, and therefore are serializable. This interface method it is called "handshake interface" through CS handshake, the server can determine three things: which files to be transmitted over the size of the file, where the file is stored, which is just three complete file Upload foundation.

Then is the "upload file content update phase." Client file open, each read a certain amount of content files, such as each read (1024 * 1024 * 2) byte of data and interface method of this batch of data transmission via the "Upload Update" to the server by server written to the output stream, and check whether the contents of the file have been transferred, if you have been transferred, perform the flush operation generates persistent file server; client after each transmission batch of data, the update "of transmitted data total "indicated value, and the total length of the file to compare calculated to obtain file upload progress in real time to the user. In summary, the client reads the local loop and transfer the contents of the file to the server until the file is uploaded content up to read.

1.2. Functional Design

1.2.1 File Upload state information related to the management

Large file upload process on the server side, the most important is the precise management of processes related to the file upload status information, for example, the total length of the file, the total number of bytes uploaded, the file storage path, and so on. But also to ensure real-time updates throughout the process of uploading data, and must not be lost, and the file is uploaded after timely removal of this information, in order to avoid excessive accumulation server failure status data.

In view of this, we have designed a class RFileUploadTransfer to achieve the above functions. Code is as follows:

/ **
 * Description: file upload process status information package type.

 * /
public class RFileUploadTransfer implements Serializable {
    private static final long serialVersionUID = 1L;
    private String fileKey;
    // Client file path
    private String srcFilePath;
    // Server upload destination file path
    private String destFilePath;
    // File Size
    private int fileLength = 0;
    // Total number of bytes have been transferred
    private int transferByteCount = 0;
    // If the file has been completely written to the disk server
    private boolean isSaveFile = false;
    private OutputStream out = null;
    
    public RFileUploadTransfer (String srcFilePath, int srcFileLength, String destFilePath) {
        this.fileKey = UUID.randomUUID () toString ().;
        this.srcFilePath = srcFilePath;
        this.fileLength = srcFileLength;
        this.destFilePath = destFilePath;
        
        File localFile = new File (this.destFilePath);
        if (localFile.getParentFile (). exists () == false) {
            localFile.getParentFile () mkdirs ().;
        }
        try {
            this.out = new FileOutputStream (localFile);
        } Catch (FileNotFoundException e) {
            e.printStackTrace ();
        }
    }
    
    public String getFileKey () {
        return fileKey;
    }
    public String getSrcFilePath () {
        return srcFilePath;
    }
    public String getDestFilePath () {
        return destFilePath;
    }
    public boolean isSaveFile () {
        return isSaveFile;
    }
    
    public void addContentBytes (byte [] bytes) {
        try {
            if (bytes == null || bytes.length == 0) {
                return;
            }
            if (this.transferByteCount + bytes.length> this.fileLength) {
                // If the length of the data has been transmitted before this batch of data + length> length of the file, it indicates that these data are the last batch of data;
                // Since there may be empty in this installment of bytes of data, so need to be screened out.
                byte [] contents = new byte [this.fileLength - this.transferByteCount];
                for (int i = 0; i                     contents [i] = bytes [i];
                }
                this.transferByteCount = this.fileLength;
                this.out.write (contents);
            } Else {
                // Description Batch data is not the last batch of data, the file has not been transferred.
                this.transferByteCount + = bytes.length;
                this.out.write (bytes);
            }
            if (this.transferByteCount> = this.fileLength) {
                this.out.flush ();
                this.isSaveFile = true;
                if (this.out! = null) {
                    try {
                        this.out.close ();
                    } Catch (IOException e) {
                        e.printStackTrace ();
                    }
                }
            }
        } Catch (Exception ex) {
            ex.printStackTrace ();
        }
    }
}

Then, build RMI service interface method implementation class in a thread-safe collection, storage management process used to transfer large files each, as follows:

/ **
* Upload File Status Monitor
* /
private Hashtable uploadFileStatusMonitor = new Hashtable ();

1.2.2 CS handshake interface design

CS handshake interface name startUploadFile, the main function is to transfer files to exchange basic information, build file upload process state control object. Its code in the interface class is as follows:

@Override
public String startUploadFile (String localFilePath, int localFileLength, String remoteFilePath) throws RemoteException {
    RFileUploadTransfer fileTransfer = new RFileUploadTransfer (localFilePath, localFileLength, remoteFilePath);
    if (this.uploadFileStatusMonitor.containsKey (fileTransfer.getFileKey ())) {
        this.uploadFileStatusMonitor.remove (fileTransfer.getFileKey ());
    }
    this.uploadFileStatusMonitor.put (fileTransfer.getFileKey (), fileTransfer);
    return fileTransfer.getFileKey ();
}

1.2.3 The contents of the file upload the updated interface design
Upload file content update interface name updateUploadProgress, the main function is to receive client transfer over the contents of a file byte [] information. Its code in the interface class is as follows:


@Override
public boolean updateUploadProgress (String fileKey, byte [] contents) throws RemoteException {
    if (this.uploadFileStatusMonitor.containsKey (fileKey)) {
        RFileUploadTransfer fileTransfer = this.uploadFileStatusMonitor.get (fileKey);
        fileTransfer.addContentBytes (contents);
        if (fileTransfer.isSaveFile ()) {
            this.uploadFileStatusMonitor.remove (fileKey);
        }
    }
    return true;
}

1.2.4 client design

The main function of the client is to open a local file, read the contents of the documents in batch byte [] information, call RMI interface method for transmission, while transmission schedule reminders. Here is the author himself uses swing developed test code, using JProgressBar of progress real-time alerts.

progressBar.setMinimum (0);
progressBar.setMaximum (100);
InputStream is = null;
try {
    File srcFile = new File (localFilePath);
    int fileSize = (int) srcFile.length ();
    String fileKey = getFileManageService () startUploadFile (localFilePath, fileSize, remoteFilePath).;
                            
    byte [] buffer = new byte [1024 * 1024 * 2];
    int offset = 0;
    int numRead = 0;
    is = new FileInputStream (srcFile);
    while (-1! = (numRead = is.read (buffer))) {
        offset + = numRead;
        getFileManageService () updateUploadProgress (fileKey, buffer).;
        double finishPercent = (offset * 1.0 / fileSize) * 100;
        progressBar.setValue ((int) finishPercent);
    }
    if (offset! = fileSize) {
        throw new IOException ( "not completely read the file" + localFilePath);
    } Else {
        progressBar.setValue (100);
    }
} Catch (Exception ex) {
    ex.printStackTrace ();
} Finally {
    try {
        if (is! = null) {
            is.close ();
        }
    } Catch (IOException e) {
        e.printStackTrace ();
    }
}
     
         
         
         
  More:      
 
- Linux --- process tracking (Linux)
- How to use OpenVPN and PrivacyIDEA build two-factor authentication for remote access (Server)
- Atheros AR8161 / AR8162 network card driver problem solving in CentOS 6.4 (Linux)
- Java multithreading easy to confuse the concept (Programming)
- SUSE Linux firewall configuration notes (Linux)
- Seven Steps to Help Google Chrome Speed - (Linux)
- JBPM6 Tutorial - taught you how to install JBPM (Linux)
- How to monitor Linux system performance Nmon (Linux)
- Ubuntu users to install the system indicator SysPeek 0.3 (Linux)
- BackTrack (BT3, BT4) Linux installation tutorial (Linux)
- Some practical tips Linux (Linux)
- Ubuntu 12.04 / 14.04 users to install software LyX document processing (Linux)
- Linux iptables port mapping settings (Server)
- Automatic Clear date directory shell script (Linux)
- Linux network cut package is not fully defragment (Linux)
- bash login and welcome message: / etc / issue, / etc / motd (Linux)
- Linux, modify the fstab file system can not start causing solve one case (Linux)
- DOM event handlers add notes (Programming)
- Getting Started with Linux system to learn: how to use tcpdump to capture TCP SYN, ACK and FIN packets (Linux)
- Dialogue UNIX:! $ # @ *% (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.