struts文件上传FormFile
2009-05-20 09:33
357 查看
uploadFfile.jsp
<%@ page language="java" pageEncoding="gbk"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
<title>JSP for UploadfileForm form</title>
</head>
<body>
<html:form action="/uploadfile" enctype="multipart/form-data">
fname : <html:text property="fname" />
<html:errors property="fname" />
<br />
fcontent : <html:file property="fcontent" />
<html:errors property="fcontent" />
<br />
<html:submit />
<html:cancel />
</html:form>
</body>
</html>
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="uploadfileForm" type="com.jinchun.form.UploadfileForm" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="uploadfileForm"
input="/uploadfile.jsp"
name="uploadfileForm"
path="/uploadfile"
scope="request"
type="com.jinchun.action.UploadfileAction" />
</action-mappings>
<message-resources parameter="com.jinchun.ApplicationResources" />
</struts-config>
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
UploadfileAction
package com.jinchun.action;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import com.jinchun.form.UploadfileForm;
public class UploadfileAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UploadfileForm uploadfileForm = (UploadfileForm) form;
FormFile Fcontent=uploadfileForm.getFcontent();
try {
byte[] buf=Fcontent.getFileData();
OutputStream out=new FileOutputStream(new File("d://"+Fcontent.getFileName()));
out.write(buf);
out.close();
System.out.println(Fcontent.getFileSize());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
UploadfileForm
package com.jinchun.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
public class UploadfileForm extends ActionForm {
private FormFile fcontent;
/** fname property */
private String fname;
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
return null;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}
public FormFile getFcontent() {
return fcontent;
}
public void setFcontent(FormFile fcontent) {
this.fcontent = fcontent;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
}
<%@ page language="java" pageEncoding="gbk"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
<title>JSP for UploadfileForm form</title>
</head>
<body>
<html:form action="/uploadfile" enctype="multipart/form-data">
fname : <html:text property="fname" />
<html:errors property="fname" />
<br />
fcontent : <html:file property="fcontent" />
<html:errors property="fcontent" />
<br />
<html:submit />
<html:cancel />
</html:form>
</body>
</html>
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="uploadfileForm" type="com.jinchun.form.UploadfileForm" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="uploadfileForm"
input="/uploadfile.jsp"
name="uploadfileForm"
path="/uploadfile"
scope="request"
type="com.jinchun.action.UploadfileAction" />
</action-mappings>
<message-resources parameter="com.jinchun.ApplicationResources" />
</struts-config>
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
UploadfileAction
package com.jinchun.action;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import com.jinchun.form.UploadfileForm;
public class UploadfileAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UploadfileForm uploadfileForm = (UploadfileForm) form;
FormFile Fcontent=uploadfileForm.getFcontent();
try {
byte[] buf=Fcontent.getFileData();
OutputStream out=new FileOutputStream(new File("d://"+Fcontent.getFileName()));
out.write(buf);
out.close();
System.out.println(Fcontent.getFileSize());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
UploadfileForm
package com.jinchun.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
public class UploadfileForm extends ActionForm {
private FormFile fcontent;
/** fname property */
private String fname;
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
return null;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}
public FormFile getFcontent() {
return fcontent;
}
public void setFcontent(FormFile fcontent) {
this.fcontent = fcontent;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
}
相关文章推荐
- 文件上传下载——通过struts的FormFile上传单个excel文件
- 基于Struts FormFile上传文件
- 基于Struts文件上传(FormFile)详解
- Struts FormFile 文件上传
- struts FormFile 上传文件
- 基于Struts FormFile上传文件—(转载于陈毓哲老师的博客)
- 文件上传下载——通过struts的FormFile上传单个excel文件
- 基于struts 的FormFile实现文件的上传功能
- struts中post方式上传文件,enctype="multipart/form-data",request.getParameter("file") 是null的问题分析
- 利用struts的FormFile上传excel文件,并导入插入数据库
- 基于Struts FormFile上传文件
- 基于struts 的FormFile实现文件的上传功能
- 基于Struts FormFile上传文件
- js提交form表单 - input file 文件上传控制上传文件的大小和格式
- apache+tomcat 集群 sturts上传文件 FormFile 序列化问题
- 文件上传字段Ext.form.field.File(扩展Text)
- 简易版 文件上传UpdateFile(Struts2版)
- 谈谈基于iframe、FormData、FileReader三种无刷新上传文件的方法
- 三种上传文件不刷新页面的方法讨论 iframe/FormData/FileReader
- struts使用html:file上传文件的时候文件名乱码解决