WebService 大文件上传, 断点续传

本文介绍了如何使用Java实现WebService接口来处理大文件的上传,并且支持断点续传功能。详细讲解了Service端和客户端的代码实现,确保文件传输的稳定性和效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值