Struts1.1实现文件上传

<form-beans>
<form-bean name="movevehicleCfgForm" type="com.huawei.agent.movevehicle.form.MovevehicleCfgForm" />
</form-beans>

<action name="movevehicleCfgForm" parameter="method" path="/move/movevehicle" type="org.springframework.web.struts.DelegatingActionProxy">
<forward name="movevehicleImportShow" path="/mv/movevehicleImport.jsp" />
</action>

  <form id="myForm" action="" method="post" enctype="multipart/form-data"style="margin-Top:0px;margin-Bottom:0px">

<input type='file' name='filename' id='filename' value="浏 览"/>

</form>

 

 

 

if (file.getFileSize() > 0) {   // 判断是否有文件上传

    FileOutputStream out = null;

    try {

      byte[] b = file.getFileData();  // 取得文件数据

      String fileName = String.valueOf(Calendar.getInstance().getTimeInMillis());

      // 得到文件扩展名

      String extName = file.getFileName().substring(file.getFileName().lastIndexOf("."));

      File outfile = new File(dir + File.separator + fileName + extName);

      out = new FileOutputStream(outfile, false);

      out.write(b); // 通过流将数据写入文件

    } catch (Exception e) {

      e.printStackTrace();

    } finally {

      if (out != null) {

        try {

          out.close(); // 关闭文件输出流

        } catch (IOException e) {

          e.printStackTrace();

        }

      }

    }

  }

posted @ 2013-07-29 08:29  lishoubin  阅读(189)  评论(0编辑  收藏  举报