使用Struts中的ActionForm类来上传图片

  1. 用actionform上传文件
  2. ===========================================================================================================================
  3. action写法
  4. public class CompanyAddAction extends Action {
  5.     public ActionForward execute(ActionMapping mapping, ActionForm form,
  6.             HttpServletRequest request, HttpServletResponse response) {
  7. //使用Struts中的UploadForm类来上传图片,也可以用其他上传图片的控件,在网络上有很多
  8.   //取得机构LOGO并上传
  9.             String encoding = request.getCharacterEncoding();
  10.             if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8"))) {
  11.                 response.setContentType("text/html;charset=gb2312");
  12.             }//将页面编码转换为 gb2312简体中文
  13.             UploadForm uf = (UploadForm) form;//将获取的form对象强制转换为UploadForm类型
  14.             FormFile file1 = uf.getOrPicDir();//调用UploadForm中的getOrPicDir()方法获取上传的文件对象赋给FormFile                                                            //类的对象
  15.             String size1 = (file1.getFileSize() + " bytes");// 调用FormFile类的对象file1的getFileSize()方发获取文                                                                        //件大小
  16.             String fileName1 = file1.getFileName();// 调用FormFile类的对象file1的getFileName()方发获取文                                                                        //件名
  17.             //System.out.println("fileName1=" + fileName1);
  18.             try {
  19.                 InputStream stream = file1.getInputStream();//创建一个输入流把file1对象读入
  20.                 String filePath = servlet.getServletContext().getRealPath("/pictureorg");//把服务器                                                                                               //中"/pictureorg"文件路径保存为字 符串
  21.                 OutputStream bos = new FileOutputStream(filePath + "/" + fileName1);
  22.                 // 建立一个上传文件的输出流,将上传文件存入web应用的根目录。
  23.                 int bytesRead = 0;
  24.                 byte[] buffer = new byte[8192];//设置缓冲区大小
  25.                 while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
  26.                     bos.write(buffer, 0, bytesRead);// 调用write()方法把文件写入服务器
  27.                 }
  28.                 
  29.                 uf.setOrPicDir(filePath + "/" + file1.getFileName());//将文件信息与文件名set入uf对象
  30.                 //System.out.println(uf.getOrPicDir());
  31.                 bos.close();
  32.                 stream.close();
  33.             } catch (Exception e) {
  34.                 System.err.print(e);
  35.                 uf.setOrPicDir("");//异常时文件信息设置为空
  36.             }
  37.             
  38. ===================================================================================================================
  39. jap中写法
  40. form可以使用普通form,<form name="form1" method="post" action="OrgAdd.do" enctype="multipart/form-data">
  41.  <input name="orPicDir" size="35" type="file">
  42. ==================================================================================================================
  43. actionform写法
  44. import javax.servlet.http.HttpServletRequest;
  45. import org.apache.struts.action.ActionErrors;
  46. import org.apache.struts.action.ActionForm;
  47. import org.apache.struts.action.ActionMapping;
  48. import org.apache.struts.upload.FormFile;
  49. /** 
  50.  * MyEclipse Struts
  51.  * Creation date: 06-13-2008
  52.  * 
  53.  * XDoclet definition:
  54.  * @struts.form name="uploadForm"
  55.  */
  56. public class UploadForm extends ActionForm {
  57.     /*
  58.      * Generated fields
  59.      */
  60.     /** picDir property */
  61.     private FormFile picDir;
  62.     /** orPicDir property */
  63.     private FormFile orPicDir;
  64.     /*
  65.      * Generated Methods
  66.      */
  67.     /** 
  68.      * Method validate
  69.      * @param mapping
  70.      * @param request
  71.      * @return ActionErrors
  72.      */
  73.     public ActionErrors validate(ActionMapping mapping,
  74.             HttpServletRequest request) {
  75.         // TODO Auto-generated method stub
  76.         return null;
  77.     }
  78.     /** 
  79.      * Method reset
  80.      * @param mapping
  81.      * @param request
  82.      */
  83.     public void reset(ActionMapping mapping, HttpServletRequest request) {
  84.         // TODO Auto-generated method stub
  85.     }
  86.     /** 
  87.      * Returns the picDir.
  88.      * @return String
  89.      */
  90.     public FormFile getPicDir() {
  91.         return picDir;
  92.     }
  93.     /** 
  94.      * Set the picDir.
  95.      * @param picDir The picDir to set
  96.      */
  97.     public void setPicDir(FormFile picDir) {
  98.         this.picDir = picDir;
  99.     }
  100.     /** 
  101.      * Returns the orPicDir.
  102.      * @return String
  103.      */
  104.     public FormFile getOrPicDir() {
  105.         return orPicDir;
  106.     }
  107.     /** 
  108.      * Set the orPicDir.
  109.      * @param orPicDir The orPicDir to set
  110.      */
  111.     public void setOrPicDir(FormFile orPicDir) {
  112.         this.orPicDir = orPicDir;
  113.     }
  114.     public void setOrPicDir(String string) {
  115.         // TODO Auto-generated method stub
  116.         
  117.     }
  118. }
  119. ======================================================================================================================
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值