1.要想使用 HTML 表单上传一个或多个文件:
(1)须把 HTML 表单的 enctype 属性设置为 multipart/form-data;multipart/form-data: 上传二进制数据,只有使用该值 ,才能完整的传递文件数据,进行上传操作;(2)须把 HTML 表单的method 属性设置为 post;
(3)需添加 <input type=“file”> 字段;
2.在 Struts 应用程序里, FileUpload 拦截器和 Jakarta Commons FileUpload 组件可以完成文件的上传.
步骤:
(1)在 Jsp 页面的文件上传表单里使用 file 标签. 如果需要一次上传多个文件, 就必须使用多个 file 标签, 但它们的名字必须是相同的
(2) 在 Action 中新添加 3 个和文件上传相关的属性. 这 3 个属性的名字必须是以下格式
[File Name] : File -被上传的文件。例如:data
[File Name]ContentType : String -上传文件的文件类型。例如:dataContentType
[File Name]FileName : String -上传文件的文件名。例如:dataFileName
如果上传多个文件, 可以使用 List;
3.FileUpload 拦截器有 3 个属性可以设置.
(1)maximumSize: 上传单个文件的最大长度(以字节为单位), 默认值为 2 MB
(2)allowedTypes: 允许上传文件的类型, 各类型之间以逗号分隔
(3)allowedExtensions: 允许上传文件扩展名, 各扩展名之间以逗号分隔
可以在 struts.xml 文件中覆盖这 3 个属性
Commons FileUpload 组件默认接受上传文件总的最大值为 2M, 可以通过在 struts 配置文件中配置常量的方式修改
单文件上传的的实例:
表单域:
<body>struts.xml文件的配置:
<s:form action="testUploadFileAction" method="post" enctype="multipart/form-data">
<s:textfield name="name" label="用户名"></s:textfield>
<s:file name="photo" label="photo"></s:file>
<s:submit value="sumbit"></s:submit>
</s:form>
</body>
<struts>action域:
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<constant name="struts.custom.i18n.resources" value="i18n"></constant>
<package name="default" namespace="/" extends="struts-default">
<action name="i18n_Action" class="com.handler.I18n_Action"
method="testI18n">
<result>/success.jsp</result>
</action>
<action name="testUploadFileAction" class="com.handler1.TestUploadFileAction"
method="testUpload">
<interceptor-ref name="defaultStack">
<param name="fileUpload.maximumSize">1024000000000</param>
<param name="fileUpload.allowedTypes">image/jpeg,image/gif,image/pjpeg,image/png,image/tmp</param>
</interceptor-ref>
<result>/success.jsp</result>
<result name="input">/register.jsp</result>
</package>
</struts>
package com.handler1;实际运行的结果:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletContext;
import org.apache.struts2.ServletActionContext;
import com.bean.User;
import com.dao.userOpt;
import com.opensymphony.xwork2.ActionSupport;
public class TestUploadFileAction extends ActionSupport{
private String name;
private File photo;//设定三个属性由Struts2内置实现文件下载
private String photoContentType;
private String photoFileName;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public File getPhoto() {
return photo;
}
public void setPhoto(File photo) {
this.photo = photo;
}
public String getPhotoContentType() {
return photoContentType;
}
public void setPhotoContentType(String photoContentType) {
this.photoContentType = photoContentType;
}
public String getPhotoFileName() {
return photoFileName;
}
public void setPhotoFileName(String photoFileName) {
this.photoFileName = photoFileName;
}
public String testUpload() throws IOException{
User u=new User();
String name1=name;
String newpath="";
String filename=photoFileName.substring(photoFileName.lastIndexOf("."));
long l=System.currentTimeMillis();
InputStream is=new FileInputStream(photo);
String newFilename=String.valueOf(l)+filename;
ServletContext application=ServletActionContext.getServletContext();
String path=application.getRealPath("/files");//获取工程的绝对路径
System.out.println(path+newFilename);
OutputStream os=new FileOutputStream(path+"/"+newFilename);
System.out.println(path);
//OutputStream os=new
byte[] b=new byte[100];//文件的复制,即上传在服务器的固定路径上
int num=is.read(b);
while(num!=-1){
os.write(b, 0, num);
num=is.read(b);
}
is.close();
os.close();
newpath="/files/"+newFilename;
u.setName(name1);
u.setPhoto(newpath);
userOpt uo=new userOpt();
boolean b1=uo.insert(u);//往数据库中添加表单中收集的数据
System.out.println(b1+"-----");
/*System.out.println(name);
System.out.println(photo.length());
System.out.println(photoContentType);
System.out.println(photoFileName);*/
return SUCCESS;
}
}
多文件上传案例:
表单域:
<body>Struts.xml配置:
<s:form action="testUploadFileAction2" method="post" enctype="multipart/form-data">
<s:textfield name="name" label="用户名"></s:textfield>
<s:file name="photo" label="photo"></s:file>
<s:file name="photo" label="photo"></s:file>
<s:file name="photo" label="photo"></s:file>
<s:submit value="sumbit"></s:submit>
</s:form>
</body>
<struts>action域:
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<constant name="struts.custom.i18n.resources" value="i18n"></constant>
<package name="default" namespace="/" extends="struts-default">
<action name="testUploadFileAction2" class="com.handler1.TestUploadFileAction2"
method="testUpload">
<interceptor-ref name="defaultStack">
<param name="fileUpload.maximumSize">10240000</param>
<param name="fileUpload.allowedTypes">image/jpeg,image/gif,image/pjpeg</param>
</interceptor-ref>
<result>/success.jsp</result>
<result name="input">/register.jsp</result>
</action>
</package>
</struts>
package com.handler1;运行结果:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletContext;
import org.apache.struts2.ServletActionContext;
import com.bean.User;
import com.dao.userOpt;
import com.opensymphony.xwork2.ActionSupport;
public class TestUploadFileAction2 extends ActionSupport{
private String name;
private File[] photo;//定义接收文件的数组
private String[] photoContentType ;//定义接收文件类型的数组
private String[] photoFileName;//定义接收文件名的数组
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public File[] getPhoto() {
return photo;
}
public void setPhoto(File[] photo) {
this.photo = photo;
}
public String[] getPhotoContentType() {
return photoContentType;
}
public void setPhotoContentType(String[] photoContentType) {
this.photoContentType = photoContentType;
}
public String[] getPhotoFileName() {
return photoFileName;
}
public void setPhotoFileName(String[] photoFileName) {
this.photoFileName = photoFileName;
}
public String testUpload() throws IOException{
for(int i=0;i<photo.length;i++){//与单文件上传的原理类似,利用遍历数组的方式,将每个文件进行上传
User u=new User();
String name1=name;
String newpath="";
String filename=photoFileName[i].substring(photoFileName[i].lastIndexOf("."));
long l=System.currentTimeMillis();
InputStream is=new FileInputStream(photo[i]);
String newFilename=String.valueOf(l)+filename;
ServletContext application=ServletActionContext.getServletContext();
String path=application.getRealPath("/files");
System.out.println(path+newFilename);
OutputStream os=new FileOutputStream(path+"/"+newFilename);
System.out.println(path);
//OutputStream os=new
byte[] b=new byte[100];
int num=is.read(b);
while(num!=-1){
os.write(b, 0, num);
num=is.read(b);
}
is.close();
os.close();
newpath="/files/"+newFilename;
u.setName(name1);
u.setPhoto(newpath);
userOpt uo=new userOpt();
boolean b1=uo.insert(u);
System.out.println(b1+"-----");
}
return SUCCESS;
}
}
在对应的工程中files文件会发现上传的文件;同时数据也添加到数据库中;