cancel
Showing results for 
Search instead for 
Did you mean: 

Java API for file upload?

agatap
Champ in-the-making
Champ in-the-making
I am writing a java-backed webscript, that should upload a file to the repository (as a custom content type).
Currently we work on Alfresco 2.1C, with Oracle/MySql.
I have a form, which sends the content type to multipart, and sends the file in the body of a POST.
Its a simple XML file with meta-data, and I thought it will be simple and straightforward task. 
However, I cant see how I can access the InputStream from a WebScript.
I need to do it from java, as there is a lot of handling once the file is uploaded -adding aspects, performing actions on it etc.

So, I need to do one of the following:
A:  how can I upload a file from the InputStream when I am inside a java backed  WebScipt?
B: Is it alternatively possible to use a servlet to do the task?
Where can I find information about how to configure the deployment descriptor for alfresco, so that my servlet is exposed? I cant find any examples about how to add servlets to my .amp file.
2 REPLIES 2

tommorris
Champ in-the-making
Champ in-the-making
Yes, I've added a servlet to alfresco to do this job, and it works fine.
But your first idea of using a java-backed webscript is better.

Use the org.apache.commons.fileupload package to help you.
Here's a code snippet that does the job:

protected Map<String, Object> executeImpl(WebScriptRequest req, WebScriptStatus status) {
    HttpServletRequest httpReq = ((WebScriptServletRequest)req).getHttpServletRequest();
    FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    List<FileItem> items = upload.parseRequest(httpReq);
    for (Iterator<FileItem> iter = items.iterator(); iter.hasNext();) {
        FileItem item = (FileItem) iter.next();

…. and so on…

Tom
http://www.ixxus.com

agatap
Champ in-the-making
Champ in-the-making
Thanks!
Smiley Very Happy