ActFramework中实现文件上传

ActFramework中实现文件上传

文件上传是一种常见的web应用功能。这篇小灶讲述如何在ActFramework中实现文件上传,包括单文件上传和多文件上传两种情况。

首先我们定义一个Model类Document用于演示单文件上传的情况:

public class Document implements SimpleBean {
    public String desc;
    public String subject;
    public File attachment;

    public Document(String subject, String desc, File attachment) {
        this.desc = desc;
        this.subject = subject;
        this.attachment = attachment;
    }
}

下面是处理单文件上传的请求响应函数:

@PostAction("/single")
public Document handleSingleFile(File file, String subject, String desc) {
    return new Document(subject, desc, file);
}

对应单文件上传的HTML Form:

<form action="/single" method="post" enctype="multipart/form-data">
  <div>
    <input name="subject" placeholder="subject">
  </div>
  <div>
    <input name="desc" placeholder="description">
  </div>
  <div>
    <input name="file" type="file" placeholder="file">
  </div>
  <div>
    <button type="submit">Submit</button>
  </div>
</form>

以上就是Act应用中处理单文件上传的一种方式。下面来看看多文件上传的处理方式。

先定义一个Model类用于演示多文件上传:

public class Archive implements SimpleBean {
    public String desc;
    public String subject;
    public File[] attachments;

    public Archive(String subject, String desc, File[] attachments) {
        this.desc = desc;
        this.subject = subject;
        this.attachments = attachments;
    }
}

处理多文件上传的请求响应函数:

@PostAction("/multi")
// Note the param type `File[]` can be changed to `List<File>`
public Archive handleMultipleFiles(File[] files, String subject, String desc) {
    return new Archive(subject, desc, files);
}

对应的多文件上传的HTML Form:

<form action="/multi" method="post" enctype="multipart/form-data">
  <div>
    <input name="subject" placeholder="subject">
  </div>
  <div>
    <input name="desc" placeholder="description">
  </div>
  <div>
    <input name="files" type="file" placeholder="file">
  </div>
  <div>
    <input name="files" type="file" placeholder="file">
  </div>
  <div>
    <input name="files" type="file" placeholder="file">
  </div>
  <div>
    <button type="submit">Submit</button>
  </div>
</form>

完整的源代码保存在码云

ActFramework: https://www.oschina.net/p/actframework

转载于:https://my.oschina.net/greenlaw110/blog/841618

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值