上传文件其实分为两个步骤:首先,将客户端传递过来的文件保存到struts.multipart.saveDir键所指定的目录下,如果没有指定,那么就保存到javax.servlet.context.tempdir环境变量所指定的目录下,然后,Action中所定义的成员变量file,实际指向的是临时目录中的临时文件,在服务器通过IO的方式,将临时文件写入到指定的服务器的目录中。
在写表单时,也应该有一些注意:
1)传递方式必须为post;
2)还要有一个属性,即enctype=“multipart/form-data”
示例:
FileUploadAction.java:
package com.suo.actions;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport {
private String username;
private File file;
private String fileFileName;//上传的文件的名字,这个命名有一定的形式,即文件名+FileName.
private String fileContentType;//上传的文件的媒体类型,这个命名有一定的形式,即文件名+ContentType.
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;
}
public String execute() throws Exception
{
/**
* 上传文件的关键就是如何从客户端发送的请求中,获取到文件。
* 在普通的Servlet中,比较麻烦,而在Struts2中,进行了类型转换,
* 直接就可以从客户端获得文件,在Action中就可以使用。
*/
String root=ServletActionContext.getRequest().getRealPath("/upload");
//建立输出流,向磁盘传递文件
OutputStream os=new FileOutputStream(new File(root,fileFileName));
//建立输入流,接收从客户端传过来得文件,这里直接进行过类型转换,直接就可以用
InputStream is=new FileInputStream(file);
System.out.println(file.getName());
System.out.println(file.getAbsolutePath());
byte[] buffer=new byte[512];
int length=0;
while((length=is.read(buffer))!=-1)
{
os.write(buffer, 0, length);
}
is.close();
os.close();
return SUCCESS;
}
}
表单:
<form action="/struts2/test1/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>
struts.xml:
<action name="fileupload" class="com.suo.actions.FileUploadAction">
<result name="success">/result/FileUploadResult.jsp</result>
</action>
若是上传的多个文件,则Action应该这样写:
package com.suo.actions;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction2 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 {
String root=ServletActionContext.getRequest().getRealPath("/upload");
for(int i=0;i<file.size();i++)
{
OutputStream os=new FileOutputStream(new File(root,fileFileName.get(i)));
InputStream is=new FileInputStream(file.get(i));
byte[] buffer=new byte[512];
int length=0;
while((length=is.read(buffer))!= -1)
{
os.write(buffer, 0, length);
}
os.close();
is.close();
}
return SUCCESS;
}
}
指定上传文件的临时存储目录和设定上传文件的大小的方法如下:
临时目录和上传文件大小这些参数都是在一个default.properties文件中设定的,这个文件我们不能直接修改,但是我们可以通过新建一个名为struts.properties的文件,来覆盖其中的某些值,如临时目录和上传文件大小。如下在struts.properties改变临时目录和上传文件的大小:
struts.multipart.saveDir=F\:/TempUpload
struts.multipart.maxSize=104857600