
求问java servlet如何获取提交的文件。
1个回答
展开全部
参考代码importjava.io.File;importjava.util.ArrayList;importjava.util.Iterator;importjava.util.List;importjavax.servlet.ServletInputStream;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importorg.apache.commons.fileupload.FileItem;importorg.apache.commons.fileupload.disk.DiskFileItemFactory;importorg.apache.commons.fileupload.servlet.ServletFileUpload;importorg.apache.struts.action.ActionForm;importorg.apache.struts.action.ActionForward;importorg.apache.struts.action.ActionMapping;importcom.dnion.contentpush.struts.BaseDispatchAction;/***提供web页面的提交的文本推送更新*@authorwxl**/publicclassWebTextPushActionextendsBaseDispatchAction{publicActionForwardexecute(ActionMappingmapping,ActionFormform,HttpServletRequestrequest,HttpServletResponseresponse)throwsException{DiskFileItemFactoryfactory=newDiskFileItemFactory();ServletFileUploadfileUpload=newServletFileUpload(factory);//设置上传文件大小的上限,-1表示无上限fileUpload.setSizeMax(-1);Listitems=newArrayList();//上传文件,解析表单中包含的文件字段和普通字段items=fileUpload.parseRequest(request);//遍历字段进行处理Iteratoriterator=items.iterator();while(iterator.hasNext()){FileItemfileItem=(FileItem)iterator.next();if(fileItem.isFormField()){//普通字段}else{//文件字段if(textfile.equals(fileItem.getFieldName())){//上传;}}}returnsuper.execute(mapping,form,request,response);}}页面中form加上属性ENCTYPE=multipart/form-data<inputname=textfiletype=filevalue=/
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询