如何使用<html:file/>标签实现将本地文件传到服务器上,谢谢了!

解决方案 »

  1.   

    那怎么实现文件上传使用struts1.2
      

  2.   

    package forms;import org.apache.struts.action.ActionForm;
    import org.apache.struts.upload.FormFile;public class FileUploadForm extends ActionForm {
        // Default bean constructor
        public FileUploadForm() { }    /**
         * The file that the user has uploaded
         */
        private FormFile file;
        public FormFile getFile() { return this.file; }
        public void setFile(FormFile file) { this.file = file; }    /**
         * The name of the file - only for displaying results
         */    private String fname;
        public String getFname() { return this.fname; }
        public void setFname(String fname) { this.fname = fname; }
        /**
         * The size of the file - only for displaying results
         */
        private String size;
        public String getSize() { return this.size; }
        public void setSize(String size) { this.size = size; }
    }//////////////////////////////////////////////////////////////////////////////
    package action;import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.io.OutputStream;import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;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 forms.FileUploadForm;public class FileUploadAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws Exception { String dir = servlet.getServletContext().getRealPath("/upload"); FileUploadForm hff = (FileUploadForm) form; FormFile file = hff.getFile(); if (file == null) {
    return mapping.findForward("success"); } // Get the name and file size
    String fname = file.getFileName();
    String size = Integer.toString(file.getFileSize()) + " bytes"; InputStream streamIn = file.getInputStream();
    OutputStream streamOut = new FileOutputStream(dir + "/" + fname); int bytesRead = 0;
    byte[] buffer = new byte[8192];
    while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {
    streamOut.write(buffer, 0, bytesRead);
    } streamOut.close();
    streamIn.close();
    // Populate the form bean with the results for display in the View
    hff.setFname(fname);
    hff.setSize(size); // Clean up our toys when done playing
    file.destroy(); // Forward to default display
    return mapping.findForward("success"); }}
    //////////////////////////////////////////////////////////////////////////////////////
        <action   path="/HtmlFile"
                  type="action.FileUploadAction"
                  name="FileUploadForm"
                  scope="session"
                  input="/index.jsp"
                  validate="false">
          <forward name="success" path="/pages/HtmlFile.jsp"/>
        </action>
    ////////////////////////////////////////////////////////////////////////<%@ page language="java" %>
    <%@ page import="org.apache.struts.action.*" %>
    <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
    <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
    <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
    <html:html>
    <head>
    <title>&lt;html:file&gt; sample code</title>
    </head>
    <body bgcolor="white"><h1>&lt;html:file&gt; sample code</h1>
    <!--
            The most important part is to declare your form's enctype
            to be "multipart/form-data", and to have an html:file
            element that maps to your ActionForm's FormFile property
    -->
    <html:form action="HtmlFile.do" enctype="multipart/form-data">        Please select the file that you would like to upload:<br />
            <html:file property="file" /><br /><br />        <html:submit /></html:form><p>
    <logic:notEmpty name="FileUploadForm" property="fname" >
        The file just uploaded was:<p>
        <ul>
          <li>Name =<bean:write name="FileUploadForm" property="fname" />
          <li>Size =<bean:write name="FileUploadForm" property="size" />
        </ul>
    </logic:notEmpty></body>
    </html:html>
    配置下就行了
      

  3.   

    书上的例子,最好的jsp文件就是HtmlFile.jsp,用struts struts_blank工程改的。