实现文件上传功能的步骤:
- 配置文件上传参数:在Strust2的配置文件struts.xml中设置maxFileSize参数,设置单个文件最大大小;以及maxRequestSize参数,设置总文件大小。
<constant name="struts.multipart.maxFileSize" value="5 * 1024 * 1024" />
<constant name="struts.multipart.maxRequestSize" value="10 * 1024 * 1024"/>
- 编写表单:在页面上添加一个用于文件上传的表单,设置属性enctype="multipart/form-data"。
<form action="upload_file" enctype="multipart/form-data" method="POST">
<input type="file" name="uploadFile" />
<input type="submit" value="上传文件" />
</form>
- 编写Action:实现文件上传功能的核心部分,主要分为单文件上传和多文件上传两种方式。
单文件上传示例:
public class UploadAction extends ActionSupport {
private File uploadFile;
private String uploadFileFileName;
private String uploadFileContentType;
public String execute() throws Exception {
// 检查是否选择了文件
if (uploadFile == null) {
addActionError("请选择要上传的文件");
return ERROR;
}
// 检查上传文件的类型
if (!"image/jpeg".equals(uploadFileContentType)) {
addActionError("上传文件必须是jpeg格式");
return ERROR;
}
// 检查上传文件的大小
if (uploadFile.length() > 1024 * 1024) {
addActionError("上传文件大小不能超过1MB");
return ERROR;
}
// 上传文件到服务器
FileOutputStream fos = new FileOutputStream("上传目录/" + uploadFileFileName);
FileInputStream fis = new FileInputStream(uploadFile);
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fis.close();
fos.close();
// 返回上传成功页面
return SUCCESS;
}
public File getUploadFile() {
return uploadFile;
}
public void setUploadFile(File uploadFile) {
this.uploadFile = uploadFile;
}
public String getUploadFileFileName() {
return uploadFileFileName;
}
public void setUploadFileFileName(String uploadFileFileName) {
this.uploadFileFileName = uploadFileFileName;
}
public String getUploadFileContentType() {
return uploadFileContentType;
}
public void setUploadFileContentType(String uploadFileContentType) {
this.uploadFileContentType = uploadFileContentType;
}
}
多文件上传示例:
public class MultiFileUploadAction extends ActionSupport {
private List<File> uploadFiles = new ArrayList<File>();
private List<String> uploadFilesFileName = new ArrayList<String>();
private List<String> uploadFilesContentType = new ArrayList<String>();
public String execute() throws Exception {
// 检查是否选择了文件
if (uploadFiles == null || uploadFiles.size() == 0) {
addActionError("请选择要上传的文件");
return ERROR;
}
// 逐个检查并上传文件
for (int i = 0; i < uploadFiles.size(); i++) {
// 检查上传文件的类型
if (!"image/jpeg".equals(uploadFilesContentType.get(i))) {
addActionError("上传文件必须是jpeg格式");
continue;
}
// 检查上传文件的大小
if (uploadFiles.get(i).length() > 1024 * 1024) {
addActionError("上传文件大小不能超过1MB");
continue;
}
// 上传文件到服务器
FileOutputStream fos = new FileOutputStream("上传目录/" + uploadFilesFileName.get(i));
FileInputStream fis = new FileInputStream(uploadFiles.get(i));
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fis.close();
fos.close();
}
// 返回上传成功页面
return SUCCESS;
}
public List<File> getUploadFiles() {
return uploadFiles;
}
public void setUploadFiles(List<File> uploadFiles) {
this.uploadFiles = uploadFiles;
}
public List<String> getUploadFilesFileName() {
return uploadFilesFileName;
}
public void setUploadFilesFileName(List<String> uploadFilesFileName) {
this.uploadFilesFileName = uploadFilesFileName;
}
public List<String> getUploadFilesContentType() {
return uploadFilesContentType;
}
public void setUploadFilesContentType(List<String> uploadFilesContentType) {
this.uploadFilesContentType = uploadFilesContentType;
}
}
- 测试:在Web容器中启动应用后,访问上传文件的页面,在表单中选择文件并上传,上传的文件保存在服务器的指定目录中。
总结:通过对Struts2框架的配置,添加文件上传的表单,编写Action的核心代码,就能比较简单地实现文件上传功能。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Struts2实现单文件或多文件上传功能 - Python技术站