# Problem mit Datei Streaming



## Nao88 (4. Feb 2011)

Versuche momentan einen Upload zu Programmieren und bekomme ein Fehler der mir nicht wirklich was sagt 

ich benutze Struts 1 das upload plugin für die Client Seite heißt plupload

hier der java code 


```
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * Handles the multi-part MIME encoded POST .
 * 
 */
public class PluploadServlet extends Action {

    static private final long serialVersionUID = 3447685998419256747L;

    static private final String RESP_SUCCESS = "{\"jsonrpc\" : \"2.0\", \"result\" : null, \"id\" : \"id\"}";

    static private final String RESP_ERROR = "{\"jsonrpc\" : \"2.0\", \"error\" : {\"code\": 101, \"message\": \"Failed to open input stream.\"}, \"id\" : \"id\"}";

    static public final String SEP = System.getProperty("file.separator");

    static public final String TMP = System.getProperty("java.io.tmpdir");

    static public final String JSON = "application/json";

    static public final int BUF_SIZE = 4096;

    /**
     * Handles an HTTP POST request from Plupload.
     * @param req The HTTP request
     * @param resp The HTTP response
     */

    public ActionForward execute(ActionMapping mapping,
                                 ActionForm inForm,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception {

        doPost(request, response);
        return null;

    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String responseString = RESP_SUCCESS;
        boolean isMultipart = ServletFileUpload.isMultipartContent(req);

        if (isMultipart) {
            ServletFileUpload upload = new ServletFileUpload();

            try {
                FileItemIterator iter = upload.getItemIterator(req);
                while (iter.hasNext()) {
                    FileItemStream item = iter.next();
                    String name = item.getFieldName();
                    InputStream input = item.openStream();

                    // Handle a form field.
                    if (item.isFormField()) {
                        System.out.println("name=" + name + ", value="
                                + Streams.asString(input));
                    }

                    // Handle a multi-part MIME encoded file.
                    else {
                        System.out.println("File field " + name + " with file name "
                                           + item.getName() + " detected.");
                        saveUploadFile(input, item);
                    }
                }
                
            } catch (Exception e) {
                responseString = RESP_ERROR;
                e.printStackTrace();
            }
        }

        // Not a multi-part MIME request.
        else {
            responseString = RESP_ERROR;
        }

        resp.setContentType(JSON);
        byte[] responseBytes = responseString.getBytes();
        resp.setContentLength(responseBytes.length);
        ServletOutputStream output = resp.getOutputStream();
        output.write(responseBytes);
        output.flush();
    }

    /**
     * 
     * @param input The input stream to read the file from
     * @param item The multi-part MIME encoded file
     */
    private void saveUploadFile(InputStream input, FileItemStream item) throws IOException {
        File localFile = new File(TMP + SEP + item.getName());
        BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(localFile));
        byte[] data = new byte[BUF_SIZE];

        int count;
        while ((count = input.read(data, 0, BUF_SIZE)) != -1) {
            output.write(data, 0, count);
        }
        
        input.close();
        output.flush();
        output.close();
    }
}
```

Beim versuch eine Datei Hochzuladen bekomme ich folgenden Fehler 


```
04.02.2011 10:34:49 org.apache.catalina.core.StandardWrapperValve invoke
SCHWERWIEGEND: Servlet.service() for servlet action threw exception
java.lang.NoSuchMethodError: org.apache.commons.fileupload.FileUpload.getItemIterator(Lorg/apache/commons/fileupload/RequestContext;)Lorg/apache/commons/fileupload/FileItemIterator;
	at org.apache.commons.fileupload.servlet.ServletFileUpload.getItemIterator(ServletFileUpload.java:148)
	at uploader.PluploadServlet2.doPost(PluploadServlet2.java:69)
	at uploader.PluploadServlet2.execute(PluploadServlet2.java:55)
	at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
	at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)
	at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
	at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
	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.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:879)
	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(Thread.java:595)
```
danke für die hilfe

Tipps oder Vorschläge willkommen :toll:


----------



## SlaterB (4. Feb 2011)

Fehlermeldung in Suche eintippen
->
ECM Place - View topic - Unable to add a document in workplace


----------



## Nao88 (7. Feb 2011)

Dake das wars ne veraltete Library neben der Neuen mist so was dummes.

Danke SlaterB


----------

