Struts1.1實現文件上傳

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

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

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

<input type='file' name='filename' id='filename' value="瀏 覽"/>post

</form>orm

 

 

 

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();

        }

      }

    }

  } ip

相關文章
相關標籤/搜索