在struts2中上传文件变得非常的简单
1、定于struts2Action类
2、页面上传文件展示:
3、利用hibernate的注解定义byte[]类型
1、定于struts2Action类
public class WorkFlowAction extends BaseAction {
private File image;
private File definition;
public String addWorkFlow() {
try {
//使用FileUtils工具类将java.io.File类型转换为byte[]类型,然后直接调用后台的业务逻辑方法存储
byte[] byteForImage = FileUtils.readFileToByteArray(image);
byte[] byteForDefinition = FileUtils.readFileToByteArray(definition);
this.workFlowService.addWorkFlow(byteForDefinition, byteForImage);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("文件没有找到");
}
return execute();
}
}2、页面上传文件展示:
<form action="workflow!addWorkFlow.action" enctype="multipart/form-data" method="post">
请选择流程定义图片:<input type="file" name="image"><br>
请选择流程定义文件: <input type="file" name="definition"><br>
<input type="submit" value="上传">
</form>3、利用hibernate的注解定义byte[]类型
@Entity
@Table(name="t_workflow")
public class WorkFlow {
@Id
@GeneratedValue
private int id;
//二进制类型
@Type(type="binary")
//指定一下长度,数据库默认的存储容量64k,所以指定一下大小否则很容易上传的文件就放不下
@Column(length=99999999)
private byte[] processImage; //流程定义图片
@Type(type="binary")
@Column(length=99999999)
private byte[] processDef;//流程定义文件
private String name;
}
本文介绍如何使用Struts2框架实现文件上传功能。通过定义Action类处理上传请求,并使用FileUtils将文件转换为byte[]存储。同时展示了上传表单的设计及数据库实体类中对byte[]类型的注解配置。
2653

被折叠的 条评论
为什么被折叠?



