java 断点续传文件到远程服务器

package com . my . file;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FileUploadTask {

    public static void main( String [] args) {
        String fromPath = "d://target.avi";
        String tmpPath = "e://temp//";
        FileUploadTask fileTask = new FileUploadTask();
        long s = System . currentTimeMillis();
        fileTask . uploadFileByPoint( fromPath , tmpPath);
        long e = System . currentTimeMillis();
        System . out . println( "all cost time :" + ( e - s) + " ms");
    }

    /**
     * upload file by point
     * @param targetPath
     * @param uploadPath
     */
    public void uploadFileByPoint( String targetPath , String uploadPath) {
        File targetFile = new File( targetPath);
        String fileName = targetFile . getName();
        int nlastPoint = fileName . lastIndexOf( ".");
        String prefix = fileName . substring( 0 , nlastPoint);
        String suffix = fileName . substring( nlastPoint);
        File uploadFile = new File( uploadPath + fileName);
        File tmpFile = new File( uploadPath + prefix + ".dat");
        // if upload file exist,rename upload file
        if ( uploadFile . exists()) {
            SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd hh.mm.ss");
            Date date = new Date();
            uploadFile . renameTo( new File( uploadPath + prefix + "_["
                    + sdf . format( date) + "]" + suffix));
        }
        long lfBytes = targetFile . length();
        long ltmpfBytes = tmpFile . length();
        System . out . println( "target file size:" + lfBytes + " bytes");
        System . out . println( "temp file size:" + ltmpfBytes + " bytes");
        FileInputStream fileIn = null;
        BufferedInputStream bufIn = null;
        RandomAccessFile randomFile = null;
        try {
            fileIn = new FileInputStream( targetFile);
            bufIn = new BufferedInputStream( fileIn);
            randomFile = new RandomAccessFile( tmpFile , "rw");
            // skip the temp file bytes
            bufIn . skip( ltmpfBytes);
            // offset file point bytes
            randomFile . seek( ltmpfBytes);
            byte [] bytes = new byte [ 1024 ];
            while ( bufIn . read( bytes) != - 1) {
                randomFile . write( bytes);
            }
        } catch ( IOException e) {
            e . printStackTrace();
        } finally {
            // release memory to free
            releaseMemory( fileIn , bufIn , randomFile);
            // upload file over,remark file size
            ltmpfBytes = tmpFile . length();
            System . out
                    . println( "upload file over size:" + ltmpfBytes + " bytes");
            // if upload over,rename the temp file
            if ( ltmpfBytes >= lfBytes) {
                tmpFile . renameTo( new File( uploadPath + fileName));
            }
        }
    }

    /**
     *  release memory to free
     * @param fileIn
     * @param bufIn
     * @param randomFile
     */
    private void releaseMemory( FileInputStream fileIn ,
            BufferedInputStream bufIn , RandomAccessFile randomFile) {
        if ( fileIn != null) {
            try {
                fileIn . close();
            } catch ( IOException e) {
                e . printStackTrace();
            }
        }
        if ( bufIn != null) {
            try {
                bufIn . close();
            } catch ( IOException e) {
                e . printStackTrace();
            }
        }
        if ( randomFile != null) {
            try {
                randomFile . close();
            } catch ( IOException e) {
                e . printStackTrace();
            }
        }
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值