基于Struts文件上传(FormFile)详解
1. 引入依赖
首先,需要在项目中引入struts-fileupload
库。这个库是用来实现文件上传功能的。在项目的pom.xml
文件中,添加以下依赖:
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts-core</artifactId>
<version>1.3.10</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts-fileupload</artifactId>
<version>1.3.10</version>
</dependency>
2. 创建表单页面
在页面中需要添加文件上传表单,创建方法如下:
<form action="uploadFile" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Upload">
</form>
3. 创建Action
在Action
中创建对应的方法,来处理文件上传和后续操作。首先需要导入FormFile
类,用于接收上传的文件。
import org.apache.struts.upload.FormFile;
然后在方法中定义FormFile
类型的变量来接收文件:
public class FileUploadAction extends Action {
private FormFile file;
// getters and setters
}
处理文件上传的方法如下:
public ActionForward uploadFile(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
FileUploadAction formBean = (FileUploadAction) form;
FormFile file = formBean.getFile();
if (file != null && file.getFileName() != null && file.getFileName().length() > 0) {
// Save file to local file system
String pathToSave = "C:\\uploads\\" + file.getFileName();
System.out.println(pathToSave);
FileOutputStream outputStream = new FileOutputStream(pathToSave);
outputStream.write(file.getFileData());
outputStream.close();
// Forward to success page
return mapping.findForward("success");
} else {
// Forward to error page
return mapping.findForward("error");
}
}
在方法中,如果文件存在并接收成功,FileOutputStream
将把文件保存到指定的本地文件系统路径中。如果文件上传失败,则转到错误页面。
示例1
以下示例演示了如何上传文件,并打印其名称:
public ActionForward uploadFile(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
FileUploadAction formBean = (FileUploadAction) form;
FormFile file = formBean.getFile();
if (file != null && file.getFileName() != null && file.getFileName().length() > 0) {
// Print file name
System.out.println("File name: " + file.getFileName());
// Forward to success page
return mapping.findForward("success");
} else {
// Forward to error page
return mapping.findForward("error");
}
}
示例2
以下示例演示了上传文件后,在JSP页面中显示上传的文件信息:
public ActionForward uploadFile(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
FileUploadAction formBean = (FileUploadAction) form;
FormFile file = formBean.getFile();
if (file != null && file.getFileName() != null && file.getFileName().length() > 0) {
// Save file to local file system
String pathToSave = "C:\\uploads\\" + file.getFileName();
System.out.println(pathToSave);
FileOutputStream outputStream = new FileOutputStream(pathToSave);
outputStream.write(file.getFileData());
outputStream.close();
// Forward to success page with uploaded file details
request.setAttribute("fileName", file.getFileName());
request.setAttribute("fileType", file.getContentType());
request.setAttribute("fileSize", file.getFileSize());
return mapping.findForward("success");
} else {
// Forward to error page
return mapping.findForward("error");
}
}
在方法中,如果文件存在并接收成功,将文件信息设置为request
对象中的属性。在成功页面中,通过以下方式,在JSP页面中显示上传的文件信息:
File name: ${fileName}<br>
File type: ${fileType}<br>
File size: ${fileSize}<br>
以上就是使用FormFile
实现文件上传的完整攻略,示例中演示了两种不同的应用场景。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:基于Struts文件上传(FormFile)详解 - Python技术站