问题描述
在JavaEE中用ssh框架怎样解决上传图片的呢?
解决方案
解决方案二:
不知到你用的是Struts1还是2给你一个1的列子jsp页面代码<%@pagecontentType="text/html;charset=GBK"%><%@tagliburi="http://struts.apache.org/tags-html"prefix="html"%><%@tagliburi="http://struts.apache.org/tags-logic"prefix="logic"%><html><head><title>multiUploadDemo</title></head><scriptlanguage="javascript">functionaddFile(){varRows=tableFiles.rows;//类似数组的RowsvarnewRow=tableFiles.insertRow(tableFiles.rows.length);//插入新的一行varCells=newRow.cells;//类似数组的CellsvarnewCell=Rows(newRow.rowIndex).insertCell(Cells.length);newCell.align="center";newCell.innerHTML="<inputtype=filename=uploadFile["+(tableFiles.rows.length-1)+"].filevalue=''/>"}functiondelFile(){if(tableFiles.rows.length>1){tableFiles.deleteRow(tableFiles.rows.length-1);}}functiondeleteOtherFiles(){while(tableFiles.rows.length>1){tableFiles.deleteRow(tableFiles.rows.length-1);}}functionsetAddDelBtnDisplay(){varobjChkFolder=document.getElementById("chkUploadFolder");varobjBtn=document.getElementById("RowAddDelBtn");if(objChkFolder.checked){objBtn.style.display="";}else{deleteOtherFiles();objBtn.style.display="none";}}functionupload(){multiUploadForm.submit();}</script><bodybgcolor="#ffffff"><html:formmethod="post"action="/multiUploadAction.do"enctype="multipart/form-data"><tableborder="1"><TBODY><tr><td>上传文件(<inputname="chkUploadFolder"type="checkbox"onclick="setAddDelBtnDisplay();"/>多文件或文件夹)</td></tr><tr><td><tableid="tableFiles"><logic:iterateid="uploadFile"name="multiUploadForm"property="myFiles"><tr><td><html:fileproperty="file"name="uploadFile"indexed="true"onchange="javascript:alert('onchange()')"/></td></tr></logic:iterate></table></td></tr><TRid="RowAddDelBtn"style="display:none;"><tdalign="center"><table><tr><td>文件夹名:<inputtype="text"name="folderName"size="10"/></td><td><inputtype="button"name="btnAddFile"value="AddFile"onclick="addFile()"/></td><td><inputtype="button"name="btnAddFile"value="DelFile"onclick="delFile()"/></td></tr></table></td></TR><TR><td><inputtype="button"name="btnUpload"value="upload"onclick="upload()"></td></TR></TBODY></table></html:form></body></html>配置文件<form-beans><form-beanname="multiUploadForm"type="test.MultiUploadForm"/></form-beans><global-exceptions/><global-forwards/><action-mappings><actionname="multiUploadForm"path="/multiUploadAction"type="test.MultiUploadAction"/></action-mappings>form表单packagetest;importjava.util.ArrayList;importjava.util.List;importorg.apache.struts.action.ActionForm;publicclassMultiUploadFormextendsActionForm{privateListmyFiles;publicMultiUploadForm(){myFiles=newArrayList();//为了能够在页面初始显示一个filemyFiles.add(newUploadFile());}publicListgetMyFiles(){returnmyFiles;}//注意这个方法的定义publicUploadFilegetUploadFile(intindex){//得到页面上数组大小n,新建n个UploadFile并且添加到List中intsize=myFiles.size();if(index>size-1){for(inti=0;i<index-size+1;i++){//等于for(inti=0;i<index-(size-1);i++)myFiles.add(newUploadFile());}}return(UploadFile)myFiles.get(index);}publicvoidsetMyFiles(ListmyFiles){this.myFiles=myFiles;}}action是packagetest;importjava.io.File;importjava.io.FileOutputStream;importjava.util.List;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importorg.apache.struts.action.Action;importorg.apache.struts.action.ActionForm;importorg.apache.struts.action.ActionForward;importorg.apache.struts.action.ActionMapping;importorg.apache.struts.upload.FormFile;publicclassMultiUploadActionextendsAction{publicActionForwardexecute(ActionMappingmapping,ActionFormform,HttpServletRequestrequest,HttpServletResponseresponse){MultiUploadFormmultiUploadForm=(MultiUploadForm)form;ListmyFiles=multiUploadForm.getMyFiles();try{for(inti=0;i<myFiles.size();i++){UploadFileuploadFile=(UploadFile)myFiles.get(i);FormFilefile=uploadFile.getFile();StringfileName=file.getFileName();StringfilePath=getServlet().getServletContext().getRealPath("/")+"upload";/*Savefileontheserver*/if(!fileName.equals("")){System.out.println("Serverpath:"+filePath);//CreatefileFilefileToCreate=newFile(filePath,fileName);//Iffiledoesnotexistscreatefileif(!fileToCreate.exists()){FileOutputStreamfileOutStream=newFileOutputStream(fileToCreate);fileOutStream.write(file.getFileData());fileOutStream.flush();fileOutStream.close();}}if(file==null){System.out.println("fileisnull");}else{//能运行到这里,就可以使用单个文件上传的方法进行上传了。循环而已System.out.println("filename:::"+file.getFileName());System.out.println("filesize:::"+file.getFileSize());}}}catch(Exceptione){e.printStackTrace();}returnnull;}}可以实现多个图片上传别忘了导comments-fileupload.jarstruts2上传也很简单,在action中配置相应的属性,就不多说了
解决方案三:
//注意这个方法的定义publicUploadFilegetUploadFile(intindex){//得到页面上数组大小n,新建n个UploadFile并且添加到List中intsize=myFiles.size();if(index>size-1){for(inti=0;i<index-size+1;i++){//等于for(inti=0;i<index-(size-1);i++)myFiles.add(newUploadFile());}}这个UploadFile是哪儿的文件呀