cancel
Showing results for 
Search instead for 
Did you mean: 

upload file

yassine_036
Champ in-the-making
Champ in-the-making
hello
i'm want to upload file in company home
but i know how can i do this with jsp api
can you help me
thank you
11 REPLIES 11

rliu
Champ in-the-making
Champ in-the-making
You can create a web script controller (I created a Java-backed webscript controller) that receives the HTTP Post request. At a very high level, here is what I had to do:

1. Create a Map<QName, Serializable> that is required of the content. If you don't know how to do this, it would be a perfect time to learn how to use the node browser and navigate to a content item. The node browser will show what properties are stored. You will have to construct something similar (if not the same) in order to proceed.

2. Iterate thru the FormFields to obtain information (ie. filename, mime type, and content). You can obtain this information from the webScriptRequest object.

                    
String mimeType = null;
Content content = null;
FormData formData = (FormData) webScriptRequest.parseContent();
FormData.FormField[] formFields = formData.getFields();
InputStream is = null;
Map<QName, Serializable> properties = new HashMap<QName, Serializable>();

for (FormData.FormField ff : formFields) {
    if ("filename".equals(ff.getName())) {
        properties.put(ContentModel.PROP_NAME, ff.getFilename());
        properties.put(ContentModel.PROP_TITLE, ff.getFilename());
        mimeType = ff.getMimetype();
        is = ff.getContent().getInputStream();
        content = ff.getContent();
    }
}

QName qName = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, (String) properties.get(ContentModel.PROP_NAME));
properties.put(ContentModel.PROP_DESCRIPTION, "Protocol document");
properties.put(ContentModel.PROP_CONTENT, content.getContent());


3. Create the node where you want it to be a content item for. In your case, you want it to be in the company home space. There are a myriad of ways to navigate to the company_home node ref. I strongly suggest reviewing the Java service APIs if your intention is to use a Java-backed webscript controller. Once you've obtained the company_home node ref, you'll have to create a node for your file to be uploaded:


ChildAssociationRef childAssociationRef = serviceRegistry.getNodeService().createNode(<—company home node ref goes here—>, ContentModel.ASSOC_CONTAINS, qName, ContentModel.TYPE_CONTENT, properties);

4. Last step is to write the content to that node:

ContentWriter contentWriter = serviceRegistry.getFileFolderService().getWriter(childAssociationRef.getChildRef());
contentWriter.setMimetype(mimeType);
contentWriter.putContent(is);

I can't say that this is the only way or the best way to do this since I'm not an Alfresco engineer, however, this code has successfully allowed me to upload files expediently and with no problems.

yassine_036
Champ in-the-making
Champ in-the-making
thank you a lot for your help
excuse me but i want to know
have i to write this source code in the same class
or it must to be in the different class
thank you
reagrds

rliu
Champ in-the-making
Champ in-the-making
It really depends on your approach. My approach was to create my own JSP that invokes the service to upload a document (pretty much of any type) to the space I need it to be in.

yassine_036
Champ in-the-making
Champ in-the-making
thank you a lot for your help
i have a place where the user can write some text and what i want to do is to create a button and when the user click on the buttom (submit)  the text that he writes transforms on the .txt and it upload on the company home
can i do this?
thank you
regards

rliu
Champ in-the-making
Champ in-the-making
Most definitely. You can use the Alfresco client or build a custom service to achieve the results desired.

The first choice requires the user to use the out-of-box functionality to create the text document in the company home space. Though you can attempt to customize the Alfresco client, there are limitations as to how much you can do. Thru the layers of customization, you may just want to build your own client and communicate via RESTful services. That leads to the second choice. 

The second choice requires intimate knowledge of Alfresco's infrastructure. We have been utilizing Java-backed webscripts to perform any of the server-side processing. Our front-end are JSPs using the latest version of YUI JavaScript framework.

yassine_036
Champ in-the-making
Champ in-the-making
thank you a lot rliu for your help
now i'm trying to start to develop and i start by some error that i have and i don't know what's the problem this is my error:
/////////////////////////////////////////////////////////////////////////////////////////
4 juin 2009 14:53:41 org.apache.catalina.loader.WebappClassLoader findResourceInternal
INFO: Illegal access: this web application instance has been stopped already.  Could not load META-INF/services/org.apache.axis.EngineConfigurationFactory.  The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.
java.lang.NullPointerException
   at org.apache.catalina.loader.WebappClassLoader.findResources(WebappClassLoader.java:996)
   at java.lang.ClassLoader.getResources(Unknown Source)
   at org.apache.commons.discovery.jdk.JDK12Hooks.getResources(JDK12Hooks.java:150)
   at org.apache.commons.discovery.resource.DiscoverResources$1.getNextResources(DiscoverResources.java:153)
   at org.apache.commons.discovery.resource.DiscoverResources$1.getNextResource(DiscoverResources.java:129)
   at org.apache.commons.discovery.resource.DiscoverResources$1.hasNext(DiscoverResources.java:116)
   at org.apache.commons.discovery.resource.names.DiscoverNamesInFile$1.getNextClassNames(DiscoverNamesInFile.java:186)
   at org.apache.commons.discovery.resource.names.DiscoverNamesInFile$1.getNextClassName(DiscoverNamesInFile.java:170)
   at org.apache.commons.discovery.resource.names.DiscoverNamesInFile$1.hasNext(DiscoverNamesInFile.java:157)
   at org.apache.commons.discovery.resource.names.NameDiscoverers$1.getNextIterator(NameDiscoverers.java:143)
   at org.apache.commons.discovery.resource.names.NameDiscoverers$1.hasNext(NameDiscoverers.java:126)
   at org.apache.commons.discovery.resource.classes.ResourceClassDiscoverImpl$1.getNextResource(ResourceClassDiscoverImpl.java:159)
   at org.apache.commons.discovery.resource.classes.ResourceClassDiscoverImpl$1.hasNext(ResourceClassDiscoverImpl.java:147)
   at org.apache.axis.configuration.EngineConfigurationFactoryFinder$1.run(EngineConfigurationFactoryFinder.java:120)
   at java.security.AccessController.doPrivileged(Native Method)
   at org.apache.axis.configuration.EngineConfigurationFactoryFinder.newFactory(EngineConfigurationFactoryFinder.java:113)
   at org.apache.axis.configuration.EngineConfigurationFactoryFinder.newFactory(EngineConfigurationFactoryFinder.java:160)
   at org.apache.axis.client.Service.getEngineConfiguration(Service.java:813)
   at org.apache.axis.client.Service.getAxisClient(Service.java:104)
   at org.apache.axis.client.Service.<init>(Service.java:113)
   at org.alfresco.webservice.authentication.AuthenticationServiceLocator.<init>(AuthenticationServiceLocator.java:16)
   at org.alfresco.webservice.util.WebServiceFactory.getAuthenticationService(WebServiceFactory.java:159)
   at org.alfresco.webservice.util.AuthenticationUtils.startSession(AuthenticationUtils.java:79)
   at org.alfresco.sample.webservice.testecriture.createImp(testecriture.java:33)
   at org.apache.jsp.stage.content.front.essai_jsp._jspService(essai_jsp.java:813)
   at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
   at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
   at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
   at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
   at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:738)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:875)
   at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
   at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
   at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
   at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
   at java.lang.Thread.run(Unknown Source)
04 juin 2009 14:53:41.750 [ERROR] [http-8084-Processor22] [org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/xulapps].[jsp]] - "Servlet.service()" pour la servlet jsp a généré une exception
java.lang.IllegalStateException
   at org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:433)
   at javax.servlet.http.HttpServletResponseWrapper.sendRedirect(HttpServletResponseWrapper.java:126)
   at org.apache.jsp.stage.content.front.essai_jsp._jspService(essai_jsp.java:842)
   at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
   at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
   at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
   at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
   at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:738)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:875)
   at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
   at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
   at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
   at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
   at java.lang.Thread.run(Unknown Source)
and this this my code i try just to start session in alfresco this is my code

public class testecriture
{
   
  
    public static void createImp() throws Exception {
    System.out.println("/////////////////////////////////////////////////////////////////////////////////////////");
       AuthenticationUtils.startSession("admin","admin");
       try{

   
      
   } catch(Throwable e) {
      System.out.println(e.toString());
   } finally{
      // End the session
      AuthenticationUtils.endSession();
      System.exit(0);
   }
}
}
whta do you think about it
and thank you for your help
regards

rliu
Champ in-the-making
Champ in-the-making
Looking at your code, there is a lot of information about Alfresco's infrastructure that you still need to become familiar with.

I highly recommend Jeff Potts' book, http://www.packtpub.com/alfresco-developer-guide/book/mid/080509wz4lps.  There's a whole chapter on exposing content via webscripting.

Writing a POJO does not exactly fit within the Alfresco's framework.

golden_eye
Champ in-the-making
Champ in-the-making
Hi i have another problem
how to change destination of uploaded file.
I am using web script for upload and i successfully upload file,but can i  want to change the default space to another,for example,i want to upload file into Company Home/Test
upload = companyhome.childByNamePath("Test").createFile( filename);
not in
upload = companyhome.createFile( filename);
How to do that?Someone with same needs?
Please.

golden_eye
Champ in-the-making
Champ in-the-making
ok the code was right
it is working for my first step.
Now i have a little more difficult question.
I want to create space while file is uploaded,depends on the time and date  when file is uploaded.
So structure would be general space Test and then subfolders created at the time of uploaded file,like 16-sep,17-sept,18-sept and so on for one month.
Can these be done?
Please some debate about these functionality?
Tnx.