commons-fileupload.jar实现文件上传

     前几天用commons-fileupload.jar做了个demo实现了文件或者图片的上传功能。在eclipse+struts+spring+jdbc.因为在做的过程中没想到别的好的方法,所以就直接在Action中回调了JDBC实现对数据库的操作。具体代码如下:

JSP页面:

<form name="uploadForm" action="upLoad.do?method=upLoadFile" enctype="multipart/form-data" method="post">
     <table align="center">
     <tr>
     名称:<br>
     <input type="text" name="name" size="15"><br>
     请选择要上传的文件:<br>
     <input type="file" name="files"><br>     
     <input type="submit" value="上传">   
    </table>
    </form>

Action类的代码如下:

private static Log loger=LogFactory.getLog(UploadAction.class);
 public ActionForward upLoadFile(ActionMapping mapping,
            ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception
    {
  String encoding =request.getCharacterEncoding();
  if((encoding!=null)&&(encoding.equalsIgnoreCase("utf-8")));
  {
   response.setContentType("text/html; charset=gb2312");
  }
  UploadActionForm upForm=(UploadActionForm)form;
  FormFile file=upForm.getFiles();

final String ss=upForm.getName().toString();
  final InputStream is=(InputStream)file.getInputStream();
  JdbcTemplate jt=(JdbcTemplate)beenFactory().getBean("jdbcDAO");//此处使用了spring封装好的jdbc Template
  jt.execute("insert into upfiles(name,files) values(?,?)", new PreparedStatementCallback()
  {
   public Object doInPreparedStatement(PreparedStatement arg0) throws SQLException, DataAccessException
   {
    try
    {
     arg0.setString(1, ss);
     arg0.setBinaryStream(2,is, is.available());
     arg0.execute();
    } catch (IOException e)
    {     
     e.printStackTrace();
    }    
    return null;
   }   
  }); 
  return mapping.findForward("success");  
    }

代码写的不全,希望对大家有帮助。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值