【发布时间】:2019-01-15 16:22:53
【问题描述】:
我尝试将一些 csv 文件上传到服务器端并处理它们并保存到数据库中,有关于在 activeweb 中上传文件的示例吗?
【问题讨论】:
我尝试将一些 csv 文件上传到服务器端并处理它们并保存到数据库中,有关于在 activeweb 中上传文件的示例吗?
【问题讨论】:
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>
</@>
我希望此代码易于理解。
【讨论】: