天天看點

struts2檔案上傳下載下傳

struts2的檔案上傳和下載下傳

檔案上傳部分:

上傳頁面 upload.jsp

<s:form action ="upload" method ="POST" enctype ="multipart/form-data" >    

                <s:file name ="file" label ="上傳檔案"/>

                <s:submit />    

        </s:form >

上傳 UploadAction.java

  private File file;

  private String fileFileName;

  private String fileContextType;

  private String root;

  private HttpServletRequest request;

  private Map session;

  @Override

  public String execute() throws Exception {

    session.put("fileFileName", fileFileName);

    //取檔案字尾名

    String lastname=null;

    int i = fileFileName.lastIndexOf('.');

    lastname=fileFileName.substring(i+1);        

    //取目前日期

    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

    Calendar calendar = Calendar.getInstance();

    String date=sdf.format(calendar.getTime());

    //10億的随機數

    java.util.Random r=new java.util.Random();    

    String radom=String.valueOf(r.nextInt(1000000000));

    fileFileName=date+radom+"."+lastname;

    session.put("fname", fileFileName);

    InputStream is=new FileInputStream(file);

    root= ServletActionContext.getRequest().getRealPath("/upload") ;//存儲到apache項目釋出目錄上

//    String root="D:\\a1/upload";//存儲到項目檔案夾裡

    session.put("fpath", root);

    File copyFile=new File(root,fileFileName);

    OutputStream os=new FileOutputStream(copyFile);

    byte[] buffer=new byte[400];

    int length=0;

    while(    (length=is.read(buffer))    >    0     ){

      os.write(buffer, 0, length);

    }

    is.close();

    os.close();

    return SUCCESS;

  }

struts.xml配置

    <action name="upload"

      class="com.enorth.mpup.action.UploadAction">

      <result name="success">uploadshow.jsp</result>

      <result name="input">index.jsp</result>

    </action>

檔案下載下傳部分:

下載下傳頁面 uploadshow.jsp

    <a    href="download.action"     target="_blank">

  <%=session.getAttribute("fileFileName") %>

    </a>

下載下傳 FileDownAction.java

  private String fileName;// 初始的通過param指定的檔案名屬性

  private String inputPath;// 指定要被下載下傳的檔案路徑

  public InputStream getInputStream() throws Exception {

    // 通過 ServletContext,也就是application 來讀取資料

    inputPath="/upload/"+(String)session.get("fname");

    return ServletActionContext.getServletContext().getResourceAsStream(inputPath);

    return SUCCESS;    

  /** 提供轉換編碼後的供下載下傳用的檔案名 */

  public String getDownloadFileName() {

    fileName=(String) session.get("fname");

    System.out.println(fileName);

    String downFileName = fileName;

    try {

      downFileName = new String(downFileName.getBytes(), "ISO8859-1");

    } catch (UnsupportedEncodingException e) {

      e.printStackTrace();

    return downFileName;

    <!-- 下載下傳現有檔案 -->

    <action name="download" class="com.enorth.mpup.action.FileDownAction">

    <!--    <param name="inputPath">/download/aaa.txt</param> -->    

      <!-- 初始檔案名    

      <param name="fileName">aaa.txt</param>-->

      <result name="success" type="stream">

        <param name="contentType">

          application/octet-stream;charset=ISO8859-1

        </param>

        <param name="inputName">inputStream</param>

        <!-- 使用經過轉碼的檔案名作為下載下傳檔案名,downloadFileName屬性    

          對應action類中的方法 getDownloadFileName() -->

        <param name="contentDisposition">

          p_w_upload;filename="${downloadFileName}"

        <param name="bufferSize">4096</param>

      </result>

以上均為部分核心檔案.

繼續閱讀