javalite - 使用 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>
</@>
我希望这段代码很容易理解。
推荐阅读
- typescript - 节点 js 打字稿 ws
- javascript - 如何在spreadJS中拦截右键单击单元格
- xamarin - 如何让 Xamarin Forms 关闭打开的滑动视图?
- scala - 为什么 Future(Failure(new Exception)) 返回成功而不是失败?
- javascript - 如何导入设置为函数的 live let 变量?
- android-studio - Android Studio 占用大量内存
- javascript - React - 将道具传递给孩子
- php - 第一个 PHP 表单似乎不起作用
- python - 未能在我的网络抓取工具中执行“显示更多评论”
- python - 如何在 Pandas 中读取 Excel,保持没有 NaN 的混合类型列?