加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

WebService 大文件上传, 断点续传

发布时间:2020-12-16 22:42:13 所属栏目:安全 来源:网络整理
导读:Service 端代码 public long upLoadFile (String content,String fileName) throws Exception { // TODO Auto-generated method stub File file = new File( "e://" ,fileName); RandomAccessFile raf = new RandomAccessFile(file, "rw" ); raf.seek(file.l

Service 端代码

public long upLoadFile(String content,String fileName) throws Exception {
        // TODO Auto-generated method stub
        File file = new File("e://",fileName);
        RandomAccessFile raf = new RandomAccessFile(file,"rw");
        raf.seek(file.length());// 先定位
        String data = content;
        byte[] bytes = Base64.decode(data);

        raf.write(bytes);
        raf.skipBytes(data.length());// 顺序写
        raf.close();

        long length = file.length();
        if (length == -1) {
            length = file.length();
        }

        return length;
    }

客户端代码

public static void main(String[] args) {
        // TODO Auto-generated method stub


        Service serviceModel = new ObjectServiceFactory()
                .create(
                        IFaBaoBusinessExternalService.class,null,"http://192.168.1.18:8080/fabao-api/webservice/businessExternalService?wsdl",null);
        XFireProxyFactory serviceFactory = new XFireProxyFactory();
        try {
            IFaBaoBusinessExternalService service = (IFaBaoBusinessExternalService) serviceFactory
                    .create(serviceModel,"http://192.168.1.18:8080/fabao-api/webservice/businessExternalService");

            int BUFFER_LENGTH = 2048;// 一次性读入大小
            int SLEEP_TIME = 250;// 循环读次数
            boolean isend = true;
            File file = new File("D:/t/YoudaoDict.exe"); 
            long startIndex = file.length(); //获取文件长度

            if (file.exists()) {
                while (isend) {

                    FileInputStream fis = null;
                    fis = new FileInputStream(file);

                    int time = 0;
                    StringBuffer sb = new StringBuffer();
                    fis.skip(startIndex);// 定位到指定的位置
                    byte[] buffer = new byte[BUFFER_LENGTH]; 
                    int count;
                    while (time < SLEEP_TIME && (count = fis.read(buffer)) != -1) { //循环读取文件流,直到文件结尾

                        sb.append(Base64.encode(buffer,0,count));

                        time ++;
                    }

                    Long index = service.upLoadFile(sb.toString(),file.getName()); 

                    startIndex = index;

                    System.err.println("已上传 "+startIndex);
                    if (startIndex >= file.length()) {
                        isend = false;
                        System.err.println("结束 ");
                    }
                    fis.close();
                }
            }


        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读