首页 > 解决方案 > 使用 activeweb 上传文件的任何示例?

问题描述

我尝试将一些csv文件上传到服务器端并处理它们并保存到数据库,关于在activeweb中上传文件的任何示例?

标签: javaliteactiveweb

解决方案


Kitchensink 示例有一个上传演示:https ://github.com/javalite/kitchensink 。

以下是可以处理多部分 POST 请求的代码示例:

public class UploadController extends AppController {

    public void index() {}

    @POST
    public void save() throws IOException {
        List<FormItem> items = multipartFormItems();
        List<String> messages = new ArrayList<String>();

        for (FormItem item : items) {
            if(item.isFile()){
                messages.add("Found file: " + item.getFileName() + " with size: " + Util.read(item.getInputStream()).length());
            }else{
                messages.add("Found field: " + item.getFieldName() + " with value: " + item.getStreamAsString());
            }
        }
        flash("messages", messages);
        redirect(UploadController.class);
    }
}

在 Freemarker 方面:

<@form controller="upload" action="save" method="post" enctype="multipart/form-data">
    Select a file to upload:<input type="file" name="file">

<input name="book" value="The Great Gatsby" type="text">
    <button>Upload File</button>
</@>

我希望这段代码很容易理解。


推荐阅读