|
阅读:1244回复:0
关于JSP Commons FileUpload 组件上传文件的一些总结
也许好东西就是需要慢慢地去发现和总结的.
以前我写的JSP系统都是采用Jsp SmartUpload 组件来解决的. 前几天客户投诉说,上传大文件的时候,浏览器无反应,甚至会崩溃.叫我帮忙解决一下并加上上传进度表示(这个暂且不表,有需要的可加我MSN:[email protected]). 立即google ,baidu 一下,才知JSp SmartUpload 适用于比较小文件的时候,而如果上传大文件的时候还得数commons FileUpload 组件. 在网上翻阅了无数篇关于这个东东的资讯. 基本上每一篇资讯都大同小异.这也难怪,网上的东西都是转来转去的. 上传文件是解决了,但是表单上的其他文本域参数却不知道如何接收.直查得我头破血流,费了一二周都没解决. 因为全部的文章都没有提到如何解决表单的其他文本数据. 直到今天才查得一个比较可行的办法. 1:如果要传参,数据量不是太大的话,可以用get 的带参方法.即 form action 里的upload.jsp后面跟上参数. 例如:upload.jsp?id=1&bid=2&uid=somebody 表单的类型不变.这样在upload.jsp 处理上传文件数据之前可以用request.getParameter("id")来获取参数. 这是一种在传参的数据量不大的情况之下比较可行的方法. 2:如果确实没有办法,必须跟文件域一起提交到上传组件里处理.据网友所说.可以采用如下代码处理. public MultipartHttpServletRequest resolveMultipart(HttpServletRequest request) throws MultipartException { DiskFileUpload fileUpload = this.fileUpload; String enc = determineEncoding(request); // use prototype FileUpload instance if the request specifies // its own encoding that does not match the default encoding if (!enc.equals(this.defaultEncoding)) { fileUpload = new DiskFileUpload(); fileUpload.setSizeMax(this.fileUpload.getSizeMax()); fileUpload.setSizeThreshold(this.fileUpload.getSizeThreshold()); fileUpload.setRepositoryPath(this.fileUpload.getRepositoryPath()); fileUpload.setHeaderEncoding(enc); } try { List fileItems = fileUpload.parseRequest(request); Map parameters = new HashMap(); Map multipartFiles = new HashMap(); for (Iterator it = fileItems.iterator(); it.hasNext();) { FileItem fileItem = (FileItem) it.next(); if (fileItem.isFormField()) { String value = null; try { value = fileItem.getString(enc); } catch (UnsupportedEncodingException ex) { logger.warn("Could not decode multipart item '" + fileItem.getFieldName() + "' with encoding '" + enc + "': using platform default"); value = fileItem.getString(); } String[] curParam = (String[]) parameters.get(fileItem.getFieldName()); if (curParam == null) { // simple form field parameters.put(fileItem.getFieldName(), new String[] { value }); } else { // array of simple form fields String[] newParam = StringUtils.addStringToArray(curParam, value); parameters.put(fileItem.getFieldName(), newParam); } } else { // multipart file field CommonsMultipartFile file = new CommonsMultipartFile(fileItem); multipartFiles.put(file.getName(), file); if (logger.isDebugEnabled()) { logger.debug("Found multipart file [" + file.getName() + "] of size " + file.getSize() + " bytes with original filename [" + file.getOriginalFilename() + "], stored " + file.getStorageDescription()); } } } /***** 注意 parameters 就是普通的text之类的字段的值 *****/ return new DefaultMultipartHttpServletRequest(request, multipartFiles, parameters); } catch (FileUploadBase.SizeLimitExceededException ex) { throw new MaxUploadSizeExceededException(this.fileUpload.getSizeMax(), ex); } catch (FileUploadException ex) { throw new MultipartException("Could not parse multipart request", ex); } } 使用方法是: MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; 然后就能正常读取参数: multipartRequest.getParameter("xxx"); 行不行,能不能获取到,我没有去验证,有验证成功的告知小弟一声,不胜感激. 以上代码的参考网址:http://dev2dev.bea.com.cn/bbs/thread.jspa?forumID=121&threadID=16067&messageID=97803 |
|
|