开发者

Get post parameter before upload

开发者 https://www.devze.com 2023-01-04 13:44 出处:网络
When I use Commons FileUpload the method parseRequest(request) uploads files and also reads additional开发者_开发问答 post parameters. So I can get parameter values only after uploading the files.

When I use Commons FileUpload the method parseRequest(request) uploads files and also reads additional开发者_开发问答 post parameters. So I can get parameter values only after uploading the files. The problem is that I need those parameter values before uploading the files (one of the parameters is upload_path).

Is there any way to get post parameters first from multipart content and then start uploading to the correct folder?

(request.getParameter(name) will return null for multipart content).

Thanks in advance!


This works for me, in first pass I check all parameters, after that I take the non form fields for download.

ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iter = upload.getItemIterator( request );

while ( iter.hasNext() ) {
    FileItemStream item = iter.next();
    String name = item.getFieldName();
    InputStream stream = item.openStream();

    if ( item.isFormField() ) {
        byte[] buffer = new byte[ 128 ];
        int len = stream.read( buffer );
        String value = new String( buffer, 0,len );
        if ( name.equals( "name" ) ) {
            filename  = value;
        }
        System.err.println( "Form field " + name + " with value " + value + " detected." );
        stream.close();
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号