`

struts实现上传下载

阅读更多
上传页面的实现代码如下
upload.jsp
<%@ page language="java" pageEncoding="UTF-8"%>

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
  <head>
    <html:base />
   
    <title>upload.jsp</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
 
  <body>
    <html:form    action="" method="post" enctype="multipart/form-data">
        <table>
            <tr>
                <td align="center" colspan="2">
                    <font size="4">Please Enter the Following Details</font>
                </td>
            </tr>
            <tr>
                <td align="left" colspan="2">
                    <font color="red"><htm:errors /></font>
                </td>
            </tr>
            <tr>
                <td align="right">
                    FileName
                </td>
                <td align="left">
                    <html:file property="theFile"></html:file>
                </td>
            </tr>
            <tr>
                <td align="center" colspan="2">
                    <html:submit>Upload File</html:submit>
                </td>
            </tr>
        </table>
    </html:form>
  </body>
</html:html>



其中struts通过表单实现页面的上传时,需要在form表单把enctype="multipart/form-data",使用了这个属性后,HttpServletRequest对象的getParameter()方法就无法直接得到用户所提交的其他数据。好在struts对这种格式编码的数据进行了处理,使得用户提交的数据仍然可以被直接设置成actionForm种对应的值。

ActionForm:



package ActionForm;

import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;

public class UploadForm extends ActionForm {
   
    private FormFile theFile;

    public FormFile getTheFile() {
        return theFile;
    }

    public void setTheFile(FormFile theFile) {
        this.theFile = theFile;
    }
}



UploadAction文件



package com.xpjjy.struts;

import java.io.File;
import java.io.FileOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import ActionForm.UploadForm;

public class UploadAction extends Action {

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception{
        UploadForm uploadForm = (UploadForm) form;
       
        FormFile file = uploadForm.getTheFile();
        String contentType = file.getContentType();
        String fileName = file.getFileName();
        int fileSize = file.getFileSize();
        byte[] fileData = file.getFileData();
       
        System.out.println("Contentype: "+ contentType);
        System.out.println("FileName: " + fileName);
        System.out.println("File Size "+ fileSize);
       
        FileOutputStream out = new FileOutputStream(new File("D:\\"+fileName));
        out.write(fileData);
        out.close();
       
        HttpSession session = request.getSession();
        session.setAttribute("contentType", contentType);
        session.setAttribute("fileName", fileName);
        session.setAttribute("fileSize", Integer.valueOf(fileSize));
       
        return mapping.findForward("download");
    }
}




httpSession的相关资料空间有

download.jsp



<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Download and Open the File</title>
</head>
<body>
    <html:link action="/download">Download</html:link>
    <br>
    <html:link action="/open">Open</html:link>
</body>
</html>




DownloadFileAction



package com.xpjjy.struts;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DownloadAction;

public class DownloadFileAction extends DownloadAction {

    protected StreamInfo getStreamInfo(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        String contentType = (String)session.getAttribute("contentType");
        String fileName = (String)session.getAttribute("fileName");
       
        response.setHeader("content-disposition", "attachment; filename="+fileName);
        File file = new File("D:\\"+ fileName);
        return new FileStreamInfo(contentType,file);
    }

   
}





Strut 的配置文件 struts-config.xml



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>
  <form-beans>
    <form-bean name="uploadForm" type="ActionForm.UploadForm" />
  </form-beans>
  <global-exceptions />
  <global-forwards />
  <action-mappings >
    <action path="/OutputXMLAction"
            type="com.xpjjy.struts.OutputXMLAction"
            scope="request"                 
    >
            <forward name="success" path="/index.jsp" />
    </action>
    <action
      attribute="uploadForm"
      input="/upload.jsp"
      name="uploadForm"
      path="/upload"
      scope="request"
      type="com.xpjjy.struts.UploadAction"
      >
        <forward name="download" path="/download.jsp" />
    </action>
   
    <action path="/download"
            type="com.xpjjy.struts.DownloadFileAction"
            scope="request"
    >
    </action>

  </action-mappings>

  <message-resources parameter="com.xpjjy.struts.ApplicationResources" />
</struts-config>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics