- 用actionform上传文件
- ===========================================================================================================================
- action写法
- public class CompanyAddAction extends Action {
- public ActionForward execute(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- //使用Struts中的UploadForm类来上传图片,也可以用其他上传图片的控件,在网络上有很多
- //取得机构LOGO并上传
- String encoding = request.getCharacterEncoding();
- if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8"))) {
- response.setContentType("text/html;charset=gb2312");
- }//将页面编码转换为 gb2312简体中文
- UploadForm uf = (UploadForm) form;//将获取的form对象强制转换为UploadForm类型
- FormFile file1 = uf.getOrPicDir();//调用UploadForm中的getOrPicDir()方法获取上传的文件对象赋给FormFile //类的对象
- String size1 = (file1.getFileSize() + " bytes");// 调用FormFile类的对象file1的getFileSize()方发获取文 //件大小
- String fileName1 = file1.getFileName();// 调用FormFile类的对象file1的getFileName()方发获取文 //件名
- //System.out.println("fileName1=" + fileName1);
- try {
- InputStream stream = file1.getInputStream();//创建一个输入流把file1对象读入
- String filePath = servlet.getServletContext().getRealPath("/pictureorg");//把服务器 //中"/pictureorg"文件路径保存为字 符串
- OutputStream bos = new FileOutputStream(filePath + "/" + fileName1);
- // 建立一个上传文件的输出流,将上传文件存入web应用的根目录。
- int bytesRead = 0;
- byte[] buffer = new byte[8192];//设置缓冲区大小
- while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
- bos.write(buffer, 0, bytesRead);// 调用write()方法把文件写入服务器
- }
- uf.setOrPicDir(filePath + "/" + file1.getFileName());//将文件信息与文件名set入uf对象
- //System.out.println(uf.getOrPicDir());
- bos.close();
- stream.close();
- } catch (Exception e) {
- System.err.print(e);
- uf.setOrPicDir("");//异常时文件信息设置为空
- }
- ===================================================================================================================
- jap中写法
- form可以使用普通form,<form name="form1" method="post" action="OrgAdd.do" enctype="multipart/form-data">
- <input name="orPicDir" size="35" type="file">
- ==================================================================================================================
- actionform写法
- 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;
- /**
- * MyEclipse Struts
- * Creation date: 06-13-2008
- *
- * XDoclet definition:
- * @struts.form name="uploadForm"
- */
- public class UploadForm extends ActionForm {
- /*
- * Generated fields
- */
- /** picDir property */
- private FormFile picDir;
- /** orPicDir property */
- private FormFile orPicDir;
- /*
- * Generated Methods
- */
- /**
- * Method validate
- * @param mapping
- * @param request
- * @return ActionErrors
- */
- public ActionErrors validate(ActionMapping mapping,
- HttpServletRequest request) {
- // TODO Auto-generated method stub
- return null;
- }
- /**
- * Method reset
- * @param mapping
- * @param request
- */
- public void reset(ActionMapping mapping, HttpServletRequest request) {
- // TODO Auto-generated method stub
- }
- /**
- * Returns the picDir.
- * @return String
- */
- public FormFile getPicDir() {
- return picDir;
- }
- /**
- * Set the picDir.
- * @param picDir The picDir to set
- */
- public void setPicDir(FormFile picDir) {
- this.picDir = picDir;
- }
- /**
- * Returns the orPicDir.
- * @return String
- */
- public FormFile getOrPicDir() {
- return orPicDir;
- }
- /**
- * Set the orPicDir.
- * @param orPicDir The orPicDir to set
- */
- public void setOrPicDir(FormFile orPicDir) {
- this.orPicDir = orPicDir;
- }
- public void setOrPicDir(String string) {
- // TODO Auto-generated method stub
- }
- }
- ======================================================================================================================
使用Struts中的ActionForm类来上传图片
最新推荐文章于 2022-11-03 12:08:01 发布