webx3文件上传

来源:互联网 发布:合肥飞友网络招聘 编辑:程序博客网 时间:2024/05/19 02:40

一、定义form

<form method="post" name="import_form" id="import_form" enctype="multipart/form-data">        <table class="table table-condensed">            $csrfToken.hiddenField            <tr>                <td>                    文件                </td>                <td>                    <input type="file" class="form-control" id="import_file" name="import_file"/>                </td>            </tr>            <tr>                <td><input type="hidden" name="action" value="importFaultAction"/></td>                <td><input type="submit" id="import_confirm" name="import_confirm" value="上传"/></td>            </tr>        </table>    </form>


二、配置webx.xml文件

Upload服务的配置参数

<services:upload sizeMax="300M"                     fileSizeMax="200M"                     repository="/tmp"                     sizeThreshold="1M"/>


参数名称说明sizeMaxHTTP请求的最大尺寸(字节,支持K/M/G),超过此尺寸的请求将被抛弃。值-1表示没有限制。fileSizeMax单个文件允许的最大尺寸(字节,支持K/M/G),超过此尺寸的文件将被抛弃。值-1表示没有限制。repository暂存上传文件的目录。 注意,这个目录是用Spring ResourceLoader装载的,而不是一个物理路径。关于|ResourceLoader,详见ResourceLoading服务的文档。sizeThreshold将文件放在内存中的阈值(字节,支持K/M/G),小于此值的文件被保存在内存中。keepFormFieldInMemory是否将普通的form field保持在内存里? 默认为false,但当sizeThreshold为0时,默认为true。

配置过滤参数

<services:request-contexts>        <request-contexts:parser URIEncoding="UTF-8" useBodyEncodingForURI="false" >            <request-contexts:filters xmlns="http://www.alibaba.com/schema/services/request-contexts/parser/filters">                <!-- 文件上传白名单 -->                <uploaded-file-whitelist extensions="xls" />            </request-contexts:filters>        </request-contexts:parser></services:request-contexts>


上面的配置将会禁止文件名后缀不在列表中的文件被上传到服务器上。如果做得更好一点,你甚至可以对上传文件进行病毒扫描。

编写Java文件

public class ImportFaultAction {    public void execute(            TurbineRunData rundata, Context context) throws IOException, ServletException {        FileItem file = rundata.getParameters().getFileItem("import_file"); //该文件为上传的文件        //InputStream in = file.getInputStream();    }}


PS:具体请参考 http://www.openwebx.org/docs/Webx3_Guide_Book.html#d0e7262

Webx框架指南: http://www.openwebx.org/docs/Webx3_Guide_Book.html

转自:http://www.hollischuang.com/archives/507

0 0
原创粉丝点击