微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

WebService 大文件上传, 断点续传

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();
        }

    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐