Search in sources :

Example 1 with FileUpload

use of jodd.upload.FileUpload in project jodd by oblac.

the class FileUploadToFileTypeConverter method convert.

public File convert(Object value) {
    if (value instanceof FileUpload) {
        FileUpload fileUpload = (FileUpload) value;
        InputStream in = null;
        try {
            in = fileUpload.getFileInputStream();
            File tempFile = FileUtil.createTempFile();
            FileUtil.writeStream(tempFile, in);
            return tempFile;
        } catch (IOException ioex) {
            return null;
        } finally {
            StreamUtil.close(in);
        }
    }
    return null;
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) File(java.io.File) FileUpload(jodd.upload.FileUpload)

Example 2 with FileUpload

use of jodd.upload.FileUpload in project jodd by oblac.

the class RequestScopeInjector method injectUploadedFiles.

/**
	 * Inject uploaded files from multipart request parameters.
	 */
protected void injectUploadedFiles(Target[] targets, ScopeData[] injectData, HttpServletRequest servletRequest) {
    if (!(servletRequest instanceof MultipartRequestWrapper)) {
        return;
    }
    MultipartRequestWrapper multipartRequest = (MultipartRequestWrapper) servletRequest;
    if (!multipartRequest.isMultipart()) {
        return;
    }
    Enumeration paramNames = multipartRequest.getFileParameterNames();
    while (paramNames.hasMoreElements()) {
        String paramName = (String) paramNames.nextElement();
        if (servletRequest.getAttribute(paramName) != null) {
            continue;
        }
        for (int i = 0; i < targets.length; i++) {
            Target target = targets[i];
            if (injectData[i] == null) {
                continue;
            }
            ScopeData.In[] scopes = injectData[i].in;
            if (scopes == null) {
                continue;
            }
            for (ScopeData.In in : scopes) {
                String name = getMatchedPropertyName(in, paramName);
                if (name != null) {
                    FileUpload[] paramValues = multipartRequest.getFiles(paramName);
                    if (ignoreInvalidUploadFiles) {
                        for (int j = 0; j < paramValues.length; j++) {
                            FileUpload paramValue = paramValues[j];
                            if ((!paramValue.isValid()) || (!paramValue.isUploaded())) {
                                paramValues[j] = null;
                            }
                        }
                    }
                    Object value = (paramValues.length == 1 ? paramValues[0] : paramValues);
                    setTargetProperty(target, name, value);
                }
            }
        }
    }
}
Also used : Enumeration(java.util.Enumeration) MultipartRequestWrapper(jodd.servlet.upload.MultipartRequestWrapper) ScopeData(jodd.madvoc.ScopeData) FileUpload(jodd.upload.FileUpload)

Example 3 with FileUpload

use of jodd.upload.FileUpload in project jodd by oblac.

the class UserAction method importList.

@Action
public String importList() throws IOException {
    stuff = "";
    for (FileUpload uploadFile : uploadFiles) {
        stuff += uploadFile.getFileContent().length;
        stuff += uploadFile.getSize();
        stuff += uploadFile.getHeader().getFileName();
        stuff += " ";
    }
    for (String uploadFileName : uploadFileNames) {
        stuff += uploadFileName;
        stuff += " ";
    }
    return "ok";
}
Also used : FileUpload(jodd.upload.FileUpload) Action(jodd.madvoc.meta.Action) MadvocAction(jodd.madvoc.meta.MadvocAction)

Example 4 with FileUpload

use of jodd.upload.FileUpload in project jodd by oblac.

the class ServletUtil method copyParamsToAttributes.

// ---------------------------------------------------------------- copy
/**
	 * Copies all request parameters to attributes.
	 */
public static void copyParamsToAttributes(HttpServletRequest servletRequest, boolean trimParams, boolean treatEmptyParamsAsNull, boolean ignoreEmptyRequestParams) {
    Enumeration paramNames = servletRequest.getParameterNames();
    while (paramNames.hasMoreElements()) {
        String paramName = (String) paramNames.nextElement();
        if (servletRequest.getAttribute(paramName) != null) {
            continue;
        }
        String[] paramValues = servletRequest.getParameterValues(paramName);
        paramValues = prepareParameters(paramValues, trimParams, treatEmptyParamsAsNull, ignoreEmptyRequestParams);
        if (paramValues == null) {
            continue;
        }
        servletRequest.setAttribute(paramName, paramValues.length == 1 ? paramValues[0] : paramValues);
    }
    // multipart
    if (!(servletRequest instanceof MultipartRequestWrapper)) {
        return;
    }
    MultipartRequestWrapper multipartRequest = (MultipartRequestWrapper) servletRequest;
    if (!multipartRequest.isMultipart()) {
        return;
    }
    paramNames = multipartRequest.getFileParameterNames();
    while (paramNames.hasMoreElements()) {
        String paramName = (String) paramNames.nextElement();
        if (servletRequest.getAttribute(paramName) != null) {
            continue;
        }
        FileUpload[] paramValues = multipartRequest.getFiles(paramName);
        servletRequest.setAttribute(paramName, paramValues.length == 1 ? paramValues[0] : paramValues);
    }
}
Also used : Enumeration(java.util.Enumeration) MultipartRequestWrapper(jodd.servlet.upload.MultipartRequestWrapper) FileUpload(jodd.upload.FileUpload)

Example 5 with FileUpload

use of jodd.upload.FileUpload in project jodd by oblac.

the class HttpBase method readBody.

/**
	 * Parses body.
	 */
protected void readBody(BufferedReader reader) {
    String bodyString = null;
    // first determine if chunked encoding is specified
    boolean isChunked = false;
    String transferEncoding = header("Transfer-Encoding");
    if (transferEncoding != null && transferEncoding.equalsIgnoreCase("chunked")) {
        isChunked = true;
    }
    // content length
    String contentLen = contentLength();
    int contentLenValue = -1;
    if (contentLen != null && !isChunked) {
        contentLenValue = Integer.parseInt(contentLen);
        if (contentLenValue > 0) {
            FastCharArrayWriter fastCharArrayWriter = new FastCharArrayWriter(contentLenValue);
            try {
                StreamUtil.copy(reader, fastCharArrayWriter, contentLenValue);
            } catch (IOException ioex) {
                throw new HttpException(ioex);
            }
            bodyString = fastCharArrayWriter.toString();
        }
    }
    // chunked encoding
    if (isChunked) {
        FastCharArrayWriter fastCharArrayWriter = new FastCharArrayWriter();
        try {
            while (true) {
                String line = reader.readLine();
                int len = Integer.parseInt(line, 16);
                if (len > 0) {
                    StreamUtil.copy(reader, fastCharArrayWriter, len);
                    reader.readLine();
                } else {
                    // end reached, read trailing headers, if there is any
                    readHeaders(reader);
                    break;
                }
            }
        } catch (IOException ioex) {
            throw new HttpException(ioex);
        }
        bodyString = fastCharArrayWriter.toString();
    }
    // no body yet - special case
    if (bodyString == null && contentLenValue != 0) {
        // body ends when stream closes
        FastCharArrayWriter fastCharArrayWriter = new FastCharArrayWriter();
        try {
            StreamUtil.copy(reader, fastCharArrayWriter);
        } catch (IOException ioex) {
            throw new HttpException(ioex);
        }
        bodyString = fastCharArrayWriter.toString();
    }
    // BODY READY - PARSE BODY
    String charset = this.charset;
    if (charset == null) {
        charset = StringPool.ISO_8859_1;
    }
    body = bodyString;
    String mediaType = mediaType();
    if (mediaType == null) {
        mediaType = StringPool.EMPTY;
    } else {
        mediaType = mediaType.toLowerCase();
    }
    if (mediaType.equals("application/x-www-form-urlencoded")) {
        form = HttpUtil.parseQuery(bodyString, true);
        return;
    }
    if (mediaType.equals("multipart/form-data")) {
        form = HttpMultiMap.newCaseInsensitveMap();
        MultipartStreamParser multipartParser = new MultipartStreamParser();
        try {
            byte[] bodyBytes = bodyString.getBytes(StringPool.ISO_8859_1);
            ByteArrayInputStream bin = new ByteArrayInputStream(bodyBytes);
            multipartParser.parseRequestStream(bin, charset);
        } catch (IOException ioex) {
            throw new HttpException(ioex);
        }
        // string parameters
        for (String paramName : multipartParser.getParameterNames()) {
            String[] values = multipartParser.getParameterValues(paramName);
            for (String value : values) {
                ((HttpMultiMap<Object>) form).add(paramName, value);
            }
        }
        // file parameters
        for (String paramName : multipartParser.getFileParameterNames()) {
            FileUpload[] uploads = multipartParser.getFiles(paramName);
            for (FileUpload upload : uploads) {
                ((HttpMultiMap<Object>) form).add(paramName, upload);
            }
        }
        return;
    }
    // body is a simple content
    form = null;
}
Also used : MultipartStreamParser(jodd.upload.MultipartStreamParser) FastCharArrayWriter(jodd.io.FastCharArrayWriter) RandomString(jodd.util.RandomString) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) FileUpload(jodd.upload.FileUpload)

Aggregations

FileUpload (jodd.upload.FileUpload)6 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 IOException (java.io.IOException)2 Enumeration (java.util.Enumeration)2 MultipartRequestWrapper (jodd.servlet.upload.MultipartRequestWrapper)2 InputStream (java.io.InputStream)1 FastCharArrayWriter (jodd.io.FastCharArrayWriter)1 ScopeData (jodd.madvoc.ScopeData)1 Action (jodd.madvoc.meta.Action)1 MadvocAction (jodd.madvoc.meta.MadvocAction)1 MultipartStreamParser (jodd.upload.MultipartStreamParser)1 RandomString (jodd.util.RandomString)1 Test (org.junit.Test)1