【问题标题】:Any example of uploading files using activeweb?使用 activeweb 上传文件的任何示例?
【发布时间】:2019-01-15 16:22:53
【问题描述】:

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

【问题讨论】:

    标签: javalite activeweb


    【解决方案1】:

    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>
    </@>
    

    我希望此代码易于理解。

    【讨论】:

    • 谢谢,@ipolevoy。很有帮助!
    • 我的声望太低,无法投票...我会在达到 15 分时投票
    猜你喜欢
    • 1970-01-01
    • 2012-09-05
    • 1970-01-01
    • 2013-07-27
    • 1970-01-01
    • 1970-01-01
    • 2015-01-07
    • 1970-01-01
    • 2014-05-04
    相关资源
    最近更新 更多