src/main/java/org/edgegallery/appstore/infrastructure/util/UploadFileUtil.java
public String uploadFile(String userId, String absolutionFilePath) {
        String imageId = "";
        File sourceFile = new File(absolutionFilePath);
        String tempFolder = new File(absolutionFilePath).getParent();
        long fileLength = sourceFile.length();

        long chunkTotal = fileLength / CHUNK_SIZE;
        if (fileLength % CHUNK_SIZE != 0) {
            chunkTotal++;
        }

        byte[] buf = new byte[CHUNK_SIZE];
        int chunkCount = 0;
        int currentChunkSize = -1;
        String identifier = UUID.randomUUID().toString().replace("-", "");
        try (RandomAccessFile readFile = new RandomAccessFile(sourceFile, "rw");) {
            while ((currentChunkSize = readFile.read(buf)) != -1) {
                chunkCount++;
                String targetFile = tempFolder + File.separator + chunkCount + ".part";
                RandomAccessFile writeFile = new RandomAccessFile(new File(targetFile), "rw");
                writeFile.write(buf, 0, currentChunkSize);
                writeFile.close();

                FileBody bin = new FileBody(new File(targetFile));
                MultipartEntityBuilder builder = MultipartEntityBuilder.create();
                builder.addPart(targetFile, bin);
                if (!sliceUploadFile(identifier, targetFile)) {
                    LOGGER.error("upload to remote file server failed.");
                    FileUtils.deleteQuietly(new File(targetFile));
                }
            }
        } catch (IOException e) {
            throw new AppException("upload to remote file server failed.", ResponseConst.RET_UPLOAD_FILE_FAILED);
        }

        if (chunkTotal == chunkCount) {
            String fileName = absolutionFilePath.substring(absolutionFilePath.lastIndexOf(File.separator) + 1);
            String uploadResult = sliceMergeFile(identifier, fileName, userId);
            Gson gson = new Gson();
            Map<String, String> uploadResultModel = gson.fromJson(uploadResult, Map.class);
            imageId = uploadResultModel.get("imageId");
            deleteTempPartFile(tempFolder, fileName);

        }
        return imageId;
    }
/**
     * upload file to file server.
     *
     * @param userId userId.
     * @param absolutionFilePath absolutionFilePath.
     */
upload file to file server.
@param userId userId.
upload file to file server . @param userId userId .
public String uploadFile(String userId, String absolutionFilePath) {
        String imageId = "";
        File sourceFile = new File(absolutionFilePath);
        String tempFolder = new File(absolutionFilePath).getParent();
        long fileLength = sourceFile.length();
        long chunkTotal = fileLength / CHUNK_SIZE;
        if (fileLength % CHUNK_SIZE != 0) {
            chunkTotal++;
        }
        byte[] buf = new byte[CHUNK_SIZE];
        int chunkCount = 0;
        int currentChunkSize = -1;
        String identifier = UUID.randomUUID().toString().replace("-", "");
        try (RandomAccessFile readFile = new RandomAccessFile(sourceFile, "rw");) {
            while ((currentChunkSize = readFile.read(buf)) != -1) {
                chunkCount++;
                String targetFile = tempFolder + File.separator + chunkCount + ".part";
                RandomAccessFile writeFile = new RandomAccessFile(new File(targetFile), "rw");
                writeFile.write(buf, 0, currentChunkSize);
                writeFile.close();
                FileBody bin = new FileBody(new File(targetFile));
                MultipartEntityBuilder builder = MultipartEntityBuilder.create();
                builder.addPart(targetFile, bin);
                if (!sliceUploadFile(identifier, targetFile)) {
                    LOGGER.error("upload to remote file server failed.");
                    FileUtils.deleteQuietly(new File(targetFile));
                }
            }
        } catch (IOException e) {
            throw new AppException("upload to remote file server failed.", ResponseConst.RET_UPLOAD_FILE_FAILED);
        }
        if (chunkTotal == chunkCount) {
            String fileName = absolutionFilePath.substring(absolutionFilePath.lastIndexOf(File.separator) + 1);
            String uploadResult = sliceMergeFile(identifier, fileName, userId);
            Gson gson = new Gson();
            Map<String, String> uploadResultModel = gson.fromJson(uploadResult, Map.class);
            imageId = uploadResultModel.get("imageId");
            deleteTempPartFile(tempFolder, fileName);
        }
        return imageId;
    }
public String uploadFile ( String userId , String absolutionFilePath ) { String imageId = "" ; File sourceFile = new File ( absolutionFilePath ) ; String tempFolder = new File ( absolutionFilePath ) . getParent ( ) ; long fileLength = sourceFile . length ( ) ; long chunkTotal = fileLength / CHUNK_SIZE ; if ( fileLength % CHUNK_SIZE != 0 ) { chunkTotal ++ ; } byte [ ] buf = new byte [ CHUNK_SIZE ] ; int chunkCount = 0 ; int currentChunkSize = - 1 ; String identifier = UUID . randomUUID ( ) . toString ( ) . replace ( "-" , "" ) ; try ( RandomAccessFile readFile = new RandomAccessFile ( sourceFile , "rw" ) ; ) { while ( ( currentChunkSize = readFile . read ( buf ) ) != - 1 ) { chunkCount ++ ; String targetFile = tempFolder + File . separator + chunkCount + ".part" ; RandomAccessFile writeFile = new RandomAccessFile ( new File ( targetFile ) , "rw" ) ; writeFile . write ( buf , 0 , currentChunkSize ) ; writeFile . close ( ) ; FileBody bin = new FileBody ( new File ( targetFile ) ) ; MultipartEntityBuilder builder = MultipartEntityBuilder . create ( ) ; builder . addPart ( targetFile , bin ) ; if ( ! sliceUploadFile ( identifier , targetFile ) ) { LOGGER . error ( "upload to remote file server failed." ) ; FileUtils . deleteQuietly ( new File ( targetFile ) ) ; } } } catch ( IOException e ) { throw new AppException ( "upload to remote file server failed." , ResponseConst . RET_UPLOAD_FILE_FAILED ) ; } if ( chunkTotal == chunkCount ) { String fileName = absolutionFilePath . substring ( absolutionFilePath . lastIndexOf ( File . separator ) + 1 ) ; String uploadResult = sliceMergeFile ( identifier , fileName , userId ) ; Gson gson = new Gson ( ) ; Map < String , String > uploadResultModel = gson . fromJson ( uploadResult , Map . class ) ; imageId = uploadResultModel . get ( "imageId" ) ; deleteTempPartFile ( tempFolder , fileName ) ; } return imageId ; }
upload file to file server.
upload file to file server .