Struts上传文件

   public class UploadFileAction extends Action {

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
UploadFileForm theForm = (UploadFileForm) form;
       
        //取得上传的文件
        FormFile file = theForm.getFiles();
        try {
            //把文件读入
            InputStream stream = file.getInputStream();
            //上传到指定的upload包中
            String filePath = request.getRealPath("/");
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            //建立一个上传文件的输出流
            OutputStream bos = new FileOutputStream(filePath + "/"
                    + file.getFileName());
           
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
                bos.write(buffer, 0, bytesRead);// 将文件写入服务器
            }
            bos.close();
            stream.close();
        } catch (Exception e) {
            System.err.print(e);
        }
return mapping.findForward("success");
    }
}


import org.apache.struts.upload.FormFile;



public class UploadFileForm extends ActionForm {
   
    private FormFile files;

    /**
     * @return the files
     */
    public FormFile getFiles() {
        return files;
    }

    /**
     * @param files the files to set
     */
    public void setFiles(FormFile files) {
        this.files = files;
    }
   
}

以上是一个文件的上传,如果是多个的上传的话,就要把ACTION中的方法如下修改
        //获得上传的文件信息
          Hashtable fileh = form.getMultipartRequestHandler().getFileElements();
          String dirPath = getServlet().getServletContext().getRealPath("/");

          if (fileh != null) {
           // 如果目录不存在,则创建
           File fpath = new File(dirPath);
           if (!fpath.exists()) {
            fpath.mkdir();
           }
           // 解析上传文件
           for (Iterator it = fileh.keySet().iterator(); it.hasNext();) {
            String key = (String) it.next();
            try {
             FormFile formfile = (FormFile) fileh.get(key);
             String filename = formfile.getFileName().trim(); // 文件名
             if (!"".equals(filename)) {

              InputStream ins = formfile.getInputStream();
              OutputStream os = new FileOutputStream(dirPath
                + File.separatorChar + filename);
              int bytesRead = 0;
              byte[] buffer = new byte[8192];
              while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
               os.write(buffer, 0, bytesRead);
              }
              os.close();
              ins.close();
             }
            } catch (Exception ex) {
             ex.printStackTrace();
            }
           }
          }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值