Hi Swati,

Please refer ContentManagementSerivce.java file  and uploadFileInterface
for more reference,
https://github.com/apache/ofbiz-framework/blob/1d92963d8d02e9ed09312b25da4a819fa731a160/applications/content/src/main/java/org/apache/ofbiz/content/ContentManagementServices.java#L645

You need to send the upload file name and bytes as per uploadFileInterface
then only you will be able to get it from request. For more detail please
refer UtilHttp.getMultiPartParameterMap method

https://github.com/apache/ofbiz-framework/blob/1d92963d8d02e9ed09312b25da4a819fa731a160/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java#L203



Kind Regards,
Deepak Dixit


On Thu, May 18, 2023 at 4:25 AM Swati Srivastava <[email protected]>
wrote:

> Hello Community.
>
>
>
> I am trying to upload a file (image/txt/word/pdf) in the ofbiz application
> using ftl and java method. After parsing the file , item listed in the list
> is coming as null. Please help.
>
>
>
> My FTL code is a simple form like below with name testUpload.ft
>
> ​
>
> <form action="uploadFile" method="post" enctype="multipart/form-data">
>
>     <input type="file" name="file" />
>
>     <input type="submit" value="Upload" />
>
> </form>
>
>
>
> My controller is configured as follows:
>
>
>
> <request-map uri="fileUplodingFtl">
>
> <security https="true" auth="true"/>
>
> <response name="success" type="view" value="fileUplodingFtl"/>
>
> </request-map>
>
>
>
> <view-map name="fileUplodingFtl" type="screen"
> page="component://xerus/widget/XerusScreens.xml#fileUplodingFtl"/>
>
>
>
> <request-map uri="uploadFile">
>
>     <security https="true" auth="true"/>
>
>     <event type="java" path="com.apache.xerus.services.FileUpload"
> invoke="uploadFile"/>
>
>     <response name="success" type="view" value="fileUplodingFtl"/>
>
> </request-map>
>
>
>
> In the java class FileUpload.java:
>
>
>
> package com.apache.xerus.services;
>
>
>
> import org.apache.commons.fileupload.FileItem;
>
> import org.apache.commons.fileupload.FileUploadException;
>
> import org.apache.commons.fileupload.disk.DiskFileItemFactory;
>
> import org.apache.commons.fileupload.servlet.ServletFileUpload;
>
> import java.io.File;
>
> import java.nio.ByteBuffer;
>
> import java.util.HashMap;
>
> import java.util.List;
>
> import java.util.Locale;
>
> import java.util.Map;
>
> import javax.servlet.ServletConfig;
>
> import javax.servlet.ServletContext;
>
> import javax.servlet.ServletException;
>
> import javax.servlet.http.HttpServletRequest;
>
> import javax.servlet.http.HttpServletResponse;
>
> import java.io.IOException;
>
> import java.net.URLEncoder;
>
> import java.util.Locale;
>
> import org.apache.commons.io.FilenameUtils;
>
> import java.io.InputStream;
>
> import java.io.OutputStream;
>
> import java.io.OutputStreamWriter;
>
> import java.io.Writer;
>
> import java.net.HttpURLConnection;
>
> import java.net.URL;
>
> import org.apache.ofbiz.base.util.Debug;
>
> import org.apache.ofbiz.base.util.GeneralException;
>
> import org.apache.ofbiz.base.util.GeneralRuntimeException;
>
> import java.util.Iterator;
>
> import org.apache.ofbiz.entity.Delegator;
>
> import org.apache.ofbiz.entity.GenericEntityException;
>
> import org.apache.ofbiz.entity.GenericValue;
>
> import org.apache.ofbiz.service.LocalDispatcher;
>
> import org.apache.ofbiz.base.util.UtilGenerics;
>
> import org.apache.ofbiz.base.util.UtilHttp;
>
> import javax.servlet.http.HttpSession;
>
> import org.apache.ofbiz.base.util.FileUtil;
>
> import org.apache.ofbiz.base.util.UtilProperties;
>
> import org.apache.ofbiz.service.ServiceUtil;
>
>
>
>
>
> public class FileUpload {
>
>
>
>     public static final String module = FileUpload.class.getName();
>
>
>
>     public static String uploadFile(HttpServletRequest request,
> HttpServletResponse response) {
>
>
>
>   // Check if the request is a multipart request
>
>         boolean isMultipart =
> ServletFileUpload.isMultipartContent(request);
>
>
>
>         if (isMultipart) {
>
>             // Create a file upload handler
>
>             DiskFileItemFactory factory = new DiskFileItemFactory();
>
>             ServletFileUpload upload = new ServletFileUpload(factory);
>
>
>
>             try {
>
>                 // Parse the request
>
>                 List<FileItem> items = upload.parseRequest(request);
>
>                 System.out.println(" Items present : "+items);
>
>                 // Process each uploaded file
>
>                 for (FileItem item : items) {
>
>                     if (!item.isFormField()) {
>
>                         // Get the uploaded file name
>
>                         String fileName = item.getName();
>
>                         System.out.println("fileName : "+fileName);
>
>                         // Get the uploaded file content
>
>                         byte[] fileContent = item.get();
>
>                         System.out.println("fileContent : "+fileContent);
>
>                         // Process the file as needed (e.g., save to disk,
> store in database, etc.)
>
>                         // ...
>
>                     }
>
>                 }
>
>             } catch (FileUploadException e) {
>
>                 Debug.logError(e, module);
>
>                 Debug.logError("[FileUpload.FileUpload] " +
> e.getMessage(), module);
>
>             }
>
>         } else {
>
>             System.out.println("It is not multipart");
>
>         }
>
>
>
>
>
> I have added the screen in XerusSceen.xml and logging in the app by
> typing localhost:8443/xerus/control/fileUplodingFtl and the page is opening
> but when I am selecting the file and pressing the Upload button, I can see
> in the logs of ofbiz  as  ' Items present: {} '. I am wondering , what is
> wrong with this code? Why is there no file present
>
>
>
> Please guide me how to achieve uploading of files in OFbiz
>
>
>
> Thank You
>
>
>
> Regards,
>
> Swati

Reply via email to