commons-fileupload上传文件(1)

         近期,写一个上传图片的功能。于是用到commons-fileupload这个组件。提过form提交表单到后台(这里没实用到structs框架)。在后台List pl = dfu.parseRequest(request); Iterator iterator = pl.iterator(); iterator的长度总是为零。而且传过去的參数不是type=file类型input。都不能用request.getParameter()获取到,这是因为在这里form提交的内容类型为“multipart/form-data”,那么如今我们必须用到parseRequest(request)这种方法。parseRequest()解释是:parseRequest方法解析出form表单中的字段的数据。将分别包装成独立的fileItem对象。然后将这些fileItem对象增加一个list类型的集合对象中。

那么全部的參数必须通过迭代iterator 获取。通过isFormField推断是不是属于一个普通表单字段。在这里刚刚提到iterator的长度为零,是由于我们没有给form里面的标签给name这个属性 比如:<input type="file" value="上传图片" name="uploadImage" />。

                demo下载:http://download.csdn.net/detail/u014458334/8895899

原文地址:https://www.cnblogs.com/jzssuanfa/p/6873767.html