1、配置Form表单:
<form action="upload.do" method="post" enctype="multipart/form-data">
标题:<input type="text" name="title"><br>
文件:<input type="file" name="myFile"><br>
<input type="submit" value="上传"><br>
</form>
2、UploadActionForm(生成相应的get,set方法)
//文件的标题
private String title;
//上传的文件,采用FormFile类型
private FormFile myFile;
3、UploadAction:
UploadActionForm uaf = (UploadActionForm)form;
String title =(String) uaf.getTitle();
FormFile myFile= uaf.getMyFile();
if(myFile != null){
//打印
System.out.println("Title"+uaf.getTitle());
System.out.println("FileName"+myFile.getFileName());
//将文件存储在磁盘上
FileOutputStream fos = new FileOutputStream("c://temp//"+myFile.getFileName());
fos.write(myFile.getFileData());
fos.flush();
fos.close();
}
return mapping.findForward("success");
4、Struts-config.xml
<form-bean name="uploadActionForm" type="com.ango.struts.UploadActionForm"/>
<action path="/upload"
type="com.ango.struts.UploadAction"
name="uploadActionForm"
scope="request">
<forward name="success" path="/upload.jsp"/>
</action>
上传的参数可以在<controller/>中配置。
5、成功转向后jsp:
标题:${uploadActionForm.title }<br>
文件名:${uploadActionForm.myFile.fileName }<br>
struts1.x上传文件配置
最新推荐文章于 2018-02-13 16:33:15 发布