1. 程式人生 > >WebService 大檔案上傳, 斷點續傳

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