文件上传
单文件上传
进行文件上传时,必须将表单的Method属性改为post,将enctype属性改为multipart/form-data
需要导入的包有:
commons-fileupload-1.3.1.jar
commons-io-2.4.jar
1、编写文件上传页面upload.jsp
<form action="fileUpload" method="post" enctype="multipart/form-data">
username:<input type="text" name="username"><br>
file:<input type="file" name="file"><br>
<input type="submit" value="submit">
</form>
2、编写FileUploadAction类,
在webContent目录下新建upload文件夹
public class FileUploadAction extends ActionSupport{
private String username;
private File file;
private String fileFileName;//struts2中文件名命名固定
private String fileContentType;//文件名类型命名固定
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
String root=ServletActionContext.getRequest().getSession().getServletContext().getRealPath("/upload");
System.out.println(root);
InputStream is=new FileInputStream(file);
File destFile=new File(root,fileFileName);
OutputStream os=new FileOutputStream(destFile);
byte[] buffer=new byte[400];
int length=0;
while(-1!=(length=is.read(buffer)))
{
os.write(buffer, 0, length);
}
is.close();
os.close();
return SUCCESS;
}
}
3、配置struts.xml文件
<struts>
<package name="struts" extends="struts-default">
<action name="fileUpload" class="cn.sict.upload.FileUploadAction">
<result name="success">/result.jsp</result>
</action>
</package>
</struts>
4、最后result.jsp输出用户名和文件名
注意:struts2在文件上传的时候,1)是将客户端的文件保存到struts.multipart.saveDir所指定的目录下,如果该目录不存在,那么就会保存到javax.servlet.context.tempdir环境变量所指定的目录中2)Action中所定义的File类型的成员变量file实际上指向的是临时目录中的临时文件,然后在服务器端通过IO的方式将临时文件写入到指定的服务器端的目录中。
可以在struts.properties下设置文件保存的临时文件<struts.multipart.saveDir=d:/test>
多文件上传
1)编写上传jsp页面
<form action="fileUpload.action" method="post" enctype="multipart/form-data">
username:<input type="text" name="username"><br>
file:<input type="file" name="file"><br>
file1:<input type="file" name="file"><br>
file2:<input type="file" name="file"><br>
<input type="submit" value="submit">
</form>
2)编写action类
public class UploadAction1 extends ActionSupport
{
private String username;
private List<File> file;
private List<String> fileFileName;
private List<String> fileContentType;
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public List<File> getFile()
{
return file;
}
public void setFile(List<File> file)
{
this.file = file;
}
public List<String> getFileFileName()
{
return fileFileName;
}
public void setFileFileName(List<String> fileFileName)
{
this.fileFileName = fileFileName;
}
public List<String> getFileContentType()
{
return fileContentType;
}
public void setFileContentType(List<String> fileContentType)
{
this.fileContentType = fileContentType;
}
@Override
public String execute() throws Exception
{
for(int i=0;i<file.size();i++)
{
InputStream is=new FileInputStream(file.get(i));
String root = ServletActionContext.getRequest().getSession()
.getServletContext().getRealPath("/upload");
File destfile=new File(root,fileFileName.get(i));
OutputStream os=new FileOutputStream(destfile);
int length=0;
byte[] buffer=new byte[400];
while(-1!=(length=is.read(buffer)))
{
os.write(buffer);
}
is.close();
os.close();
}
return SUCCESS;
}
}
3)配置struts.xml文件
<package name="struts" extends="struts-default">
<action name="fileUpload" class="cn.sict.action.UploadAction1">
<result name="success">/result1.jsp</result>
</action>
</package>
4)显示结果页面。
<body>
<s:property value="username"/><br>
<s:iterator value="fileFileName" id="f">
file:<s:property value="#f"/>//#是OGNL的操作符,如果不是根元素,就#来去特定的元素。
</s:iterator>
</body>
文件上传是的大小限制问题
Struts2框架的defaultStack中有默认的fileUpload拦截器,所以在以上程序中只要配置了默认拦截器就可以了。在默认拦截器中文件上传的大小最大不能超过2M,这一参数值配置在struts2表下的defalut.properties中struts.multipart.maxSize=2097152,那如果要修改这一参数,修改的方法如下:
全局方法:
1)在struts.properties中重新对maxSize赋值,假如修改为10M,添加内容如下:
struts.multipart.maxSize=10485760
2)在sturts.xml文件中配置<constant>标签
<constant name="struts.multipart.maxSize" value="10485760"></constant>
全局的方法对项目的文件上传的大小都给出了统一的限制大小。上面两种方法中第一种方法的优先级更高。
局部方法:
<struts>
<package name="struts" extends="struts-default">
<action name="fileUpload" class="cn.sict.upload.FileUploadAction">
<interceptor-ref name="defaultStack">
<param name="fileUpload.maximumSize">1024</param>
</interceptor-ref>
<result name="success">/result.jsp</result>
<result name="input">/error.jsp</result>
</action>
</package>
</struts>