upload a file and check by java event

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

upload a file and check by java event

Fai Kwong
Dear ofbiz,

I would like to attach a file and check that by a java event. I have done that by the example module.

https://weberp.dyndns-server.com:8443/example/control/main

I have created the servlet page for the web and done some code in controller that call the java event, but my problem is that how the java file can read that attachment file?

Here I also attached some of my code.

Attache.ftl
<form method="post" enctype="multipart/form-data" action="<@ofbizUrl>AttacheUpload</@ofbizUrl>">
        <table cellspacing="0" class="basic-table">
                    <input type="file" size="50" name="fname">
                    <input type="submit" class="smallSubmit" value="${uiLabelMap.ProductUploadImage}">   
        </table>
    </form>

controller.xml
  <request-map uri="AttacheUpload">
  <security https="true" auth="true"/>
    <event type="java" path="com.mano.license.servlet.AttacheServlet" invoke="doPost"/>
  <response name="success" type="view" value="AttacheChecker"/>
  </request-map>
  <view-map name="main" type="screen" page="component://example/widget/example/AttacheScreens.xml#main"/>


build.xml
        <fileset dir="../../framework/base/lib/j2eespecs" includes="*.jar"/>
        <fileset dir="../../hot-deploy/example/lib" includes="*.jar"/>


AttacheScreens.xml
<decorator-section name="body">
                         <platform-specific>
                            <html>
                                <html-template location="component://example/webapp/example/Attache/Attache.ftl"/>
                            </html>
                        </platform-specific>
               
                    </decorator-section>  





Seem I can call the java file but I am not sure how can I write the java code to read that attached file.

AttachServlet.java

package com.mano.Attach.servlet;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.oreilly.servlet.MultipartWrapper;

import de.schlichtherle.Attach.AttachContent;
import de.schlichtherle.Attach.AttachManager;

import com.mano.Attach.service.AttachUtil;



import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.StringUtil;
import org.ofbiz.base.util.UtilHttp;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.base.util.cache.UtilCache;
import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.security.Security;
import org.ofbiz.base.util.UtilDateTime;
// import org.ofbiz.base.util.UtilParse;
import org.ofbiz.base.util.UtilProperties;
import org.ofbiz.entity.GenericPK;
import org.ofbiz.entity.condition.EntityCondition;
import org.ofbiz.entity.condition.EntityOperator;
import org.ofbiz.entity.transaction.GenericTransactionException;
import org.ofbiz.entity.transaction.TransactionUtil;
import org.ofbiz.entity.util.EntityListIterator;
import org.ofbiz.entity.util.EntityUtil;
//import org.ofbiz.product.store.ProductStoreWorker;
import org.ofbiz.service.GenericServiceException;
import org.ofbiz.service.LocalDispatcher;
/**
 * Servlet for installing Attach.
 *
 * @author Manohar Viswanathan
 */
public class AttachServlet extends HttpServlet {


   
    private static final long serialVersionUID = 1L;

   
//    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
 
       
        LocalDispatcher dispatcher = (LocalDispatcher)request.getAttribute("dispatcher");
        GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
       
       
        //PrintWriter out = null;
        try {
            System.out.println("testing");
            MultipartWrapper multipart = ((MultipartWrapper) request);
        File AttachFile = multipart.getFile("AttachFile");
            AttachManager lm = new AttachManager(AttachUtil.licenceParam);
            lm.install(AttachFile);
            AttachContent lc = lm.verify();
            System.out.println("AttachServlet:: Attach valid till " + lc.getNotAfter());
           
            response.setContentType("text/html");
           out = response.getWriter();
           out.println("New Attach installed. <a href='Attach.jsp'>Continue</a>");
        } catch (Exception e) {
            throw new ServletException(e);
           
        } finally {
            close(out);
        }
    }
   
     //Utility method
    private void close(Closeable closer) {
        if (closer != null) {
            try {
                closer.close();
            }
            catch (IOException ignore) {
             
            }
        }
    }
   
}







Looking for your reply

Yours,
Ivan