在JavaEE中用ssh框架怎样解决上传图片的呢?

解决方案 »

  1.   

    不知到你用的是Struts1还是2
    给你一个1的列子
    jsp页面代码
    <%@ page contentType="text/html; charset=GBK"%>
    <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
    <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
    <html>
    <head>
        <title>multiUploadDemo</title>
    </head>
    <script language="javascript" >
        function addFile(){
            var Rows=tableFiles.rows;//类似数组的Rows 
            var newRow=tableFiles.insertRow(tableFiles.rows.length);//插入新的一行 
             var Cells=newRow.cells;//类似数组的Cells 
            var newCell=Rows(newRow.rowIndex).insertCell(Cells.length); 
            newCell.align="center"; 
            newCell.innerHTML= "<input type=file name=uploadFile["+ (tableFiles.rows.length-1) + "].file value=''/>"
        }    function delFile(){
            if (tableFiles.rows.length > 1){ 
                tableFiles.deleteRow(tableFiles.rows.length-1); 
            }
        }  
        function deleteOtherFiles(){
            while(tableFiles.rows.length > 1){ 
                tableFiles.deleteRow(tableFiles.rows.length-1); 
            }
        }
       function setAddDelBtnDisplay(){
               var objChkFolder = document.getElementById("chkUploadFolder");
               var objBtn = document.getElementById("RowAddDelBtn");
               if(objChkFolder.checked){
                   objBtn.style.display="";
               }else{
                deleteOtherFiles();
                   objBtn.style.display="none";
               }
    }   
       function upload(){
         multiUploadForm.submit();
       }
    </script>
    <body bgcolor="#ffffff">
        <html:form method="post" action="/multiUploadAction.do" enctype="multipart/form-data">
            <table border="1">
                <TBODY>
                <tr>
                    <td>
                        上传文件(<input name="chkUploadFolder" type="checkbox" onclick="setAddDelBtnDisplay();"/>多文件或文件夹)
                    </td>
                </tr>
                <tr>
                    <td>
                        <table id="tableFiles">
                             <logic:iterate id="uploadFile" name="multiUploadForm" property="myFiles">
                             <tr>
                                        <td>
                                            <html:file property="file" name="uploadFile" indexed="true" onchange="javascript:alert('onchange()')"/>
                                        </td>
                                    </tr>
                             </logic:iterate>
                        </table>
                    </td>
                </tr>
                <TR id ="RowAddDelBtn" style="display: none;">
                    <td align="center">
                        <table>
                            <tr>
                                <td>
                                    文件夹名:<input type="text" name="folderName" size="10"/>
                                </td>
                                <td>
                                    <input type="button" name="btnAddFile" value="Add File" onclick="addFile()" />
                                </td>
                                <td>
                                    <input type="button" name="btnAddFile" value="Del File" onclick="delFile()" />
                                </td>
                            </tr>
                        </table>
                    </td>
                </TR>
                <TR>
                    <td>
                        <input type="button" name="btnUpload" value="upload" onclick="upload()">
                    </td>
                </TR>
                </TBODY>
            </table>
        </html:form>
    </body>
    </html>
    配置文件
      <form-beans>
       <form-bean name="multiUploadForm" type="test.MultiUploadForm" />    
      </form-beans>
      <global-exceptions />
      <global-forwards />
      
      <action-mappings>
       <action name="multiUploadForm" path="/multiUploadAction" type="test.MultiUploadAction" />
      </action-mappings>
    form表单
    package test;import java.util.ArrayList;
    import java.util.List;import org.apache.struts.action.ActionForm;public class MultiUploadForm extends ActionForm
    {
    private List myFiles;
        public MultiUploadForm() {
            myFiles = new ArrayList();
            //为了能够在页面初始显示一个file
            myFiles.add(new UploadFile());
        }
        public List getMyFiles() {
            return myFiles;
        }
        //注意这个方法的定义 
        public UploadFile getUploadFile(int index) {//得到页面上数组大小n,新建n个UploadFile并且添加到List中
            int size = myFiles.size();
            if (index > size - 1) {
                for (int i = 0; i < index - size + 1; i++) {//等于 for(int i=0;i<index-(size-1);i++)
                    myFiles.add(new UploadFile());
                }
            }
            return (UploadFile) myFiles.get(index);
        }
        public void setMyFiles(List myFiles) {
            this.myFiles = myFiles;
        }
    }
    action是
    package test;import java.io.File;
    import java.io.FileOutputStream;
    import java.util.List;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;public class MultiUploadAction extends Action
    {
    public ActionForward execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
    {
       MultiUploadForm multiUploadForm = (MultiUploadForm) form;
       List myFiles = multiUploadForm.getMyFiles();
       try
       {
        for (int i = 0; i < myFiles.size(); i++)
        {
         UploadFile uploadFile = (UploadFile) myFiles.get(i);
         FormFile file = uploadFile.getFile();
         String fileName = file.getFileName();
         String filePath = getServlet().getServletContext().getRealPath(
           "/")
           + "upload";
         /* Save file on the server */
         if (!fileName.equals(""))
         {
          System.out.println("Server path:" + filePath);
          //Create file
          File fileToCreate = new File(filePath, fileName);
          //If file does not exists create file                      
          if (!fileToCreate.exists())
          {
           FileOutputStream fileOutStream = new FileOutputStream(
             fileToCreate);
           fileOutStream.write(file.getFileData());
           fileOutStream.flush();
           fileOutStream.close();
          }
         }
         if (file == null)
         {
          System.out.println("file is null");
         } else
         {
          //能运行到这里,就可以使用单个文件上传的方法进行上传了。循环而已
          System.out.println("filename:::" + file.getFileName());
          System.out.println("file size:::" + file.getFileSize());
         }
        }
       } catch (Exception e)
       {
        e.printStackTrace();
       }
       return null;
    }
    }
    可以实现多个图片上传
    别忘了导
    comments-fileupload.jarstruts2上传也很简单,在action中配置相应的属性,就不多说了
      

  2.   

     //注意这个方法的定义  
      public UploadFile getUploadFile(int index) {//得到页面上数组大小n,新建n个UploadFile并且添加到List中
      int size = myFiles.size();
      if (index > size - 1) {
      for (int i = 0; i < index - size + 1; i++) {//等于 for(int i=0;i<index-(size-1);i++)
      myFiles.add(new UploadFile());
      }
      }这个UploadFile是哪儿的文件呀