struts文件上传

在这里用到了java  的一个开源jar 包:叫做upload 是用来做上传用的。在struts中使用:

在下面就一个实例来说明这个jar包的用法。

说到上上传第一肯定要有一个上传点。我用了一个struts的一个

<html:file property="files"/>

获得一个文件的路。

第二部就是用一个Action得到文件路径。并进行上传:代码如下:

public ActionForward execute(ActionMapping mapping, ActionForm form,

           HttpServletRequest request, HttpServletResponse response) {

       UploadForm uploadForm = (UploadForm) form;// TODO Auto-generated method stub

       FormFile formFile = uploadForm.getFiles();

       try {

           InputStream input = formFile.getInputStream();

            String rootpath = request.getSession().getServletContext().getRealPath("/upload");

           OutputStream out = new FileOutputStream(rootpath+"/"+formFile.getFileName());

          

           byte[] buffer = new byte[1024];

          

           while(input.available()>1024){

              input.read(buffer);

              out.write(buffer);

           }

           buffer = new byte[input.available()];

           input.read(buffer);

           out.write(buffer);

          

           out.close();

           input.close();

       } catch (IOException e) {

           e.printStackTrace();

           return mapping.findForward("fail");

       }

       return mapping.findForward("success");

    }

其中特红部分代表目标地址:全路径:

上面的路径是:

D:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\strutsupload\upload\

下面有一实例。可参考:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值