Upload web document/file in to liferay from external java application

Refer https://web.liferay.com/web/antonio.junior/blog/-/blogs/12168124 and create web service client for Portlet_DL_DLAppService.

 

package com.lr.ws.soap.fileupload;
 
import java.net.MalformedURLException;
import java.net.URL;
 
import com.liferay.portal.service.ServiceContext;
import com.liferay.portlet.documentlibrary.service.http.DLAppServiceSoap;
import com.liferay.portlet.documentlibrary.service.http.DLAppServiceSoapServiceLocator;
 
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
 
public class DLFileUpload {
public static void main(String[] args) {
        try {
        DLAppServiceSoapServiceLocator dlAppLocator = new DLAppServiceSoapServiceLocator();
            DLAppServiceSoap dlAppService = dlAppLocator.getPortlet_DL_DLAppService(getURL("Portlet_DL_DLAppService"));
        ServiceContext serviceContext = new ServiceContext();
       
        Path path = Paths.get("path of file");
        byte[] bytes = Files.readAllBytes(path);
       
         dlAppService.addFileEntry(repositoryId, folderId, sourceFileName, mimeType, title, description, changeLog, bytes, serviceContext);
            System.out.println("-------------------FILE UPLOADED-------------------------------");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    private static URL getURL(String serviceName) throws MalformedURLException {
        String url = "http://localhost:8080";
        String screenName = "userName";
        String password = "Password";
 
        int pos = url.indexOf("://");
        String protocol = url.substring(0, pos + 3);
        String host = url.substring(pos + 3, url.length());
 
        StringBuilder sb = new StringBuilder();
        sb.append(protocol);
        sb.append(screenName);
        sb.append(":");
        sb.append(password);
        sb.append("@");
        sb.append(host);
        //sb.append("/api/secure/axis/");
        sb.append("/api/axis/");
        sb.append(serviceName);
        System.out.println("URL "+sb.toString());
        return new URL(sb.toString());
    }
}