首页 > 代码库 > 转:upload.parseRequest为空
转:upload.parseRequest为空
FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); upload.setHeaderEncoding("UTF-8"); List items = upload.parseRequest(request);
上传是items一直是空list。导致原因是struts2把原始的原来S2为简化上传功能,把所有的enctype="multipart/form-data"表单做了wrapper最后把HttpServletResquest封装成 org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper 怪不得我的 ServletFileUpload.parseRequest(request)不行!!!
看我怎么改!废话不多说,直接贴代码
- MultiPartRequestWrapper wrapper = (MultiPartRequestWrapper) request;
- File file = wrapper.getFiles("imgFile")[0];
- String fileName = wrapper.getFileNames("imgFile")[0];
- //检查文件大小
- if(file.length() > maxSize){
- String temStr= "上传文件大小超过限制。";
- this.writeResponse(response, temStr);
- return;
- }
- //检查扩展名
- String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
- if(!Arrays.<String>asList(extMap.get(dirName).split(",")).contains(fileExt)){
- String temStr= "上传文件扩展名是不允许的扩展名。\n只允许" + extMap.get(dirName) + "格式。";
- this.writeResponse(response, temStr);
- return;
- }
- SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
- String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;
- try {
- InputStream in = new FileInputStream(file);
- File uploadFile = new File(savePath, newFileName);
- OutputStream out = new FileOutputStream(uploadFile);
- byte[] buffer = new byte[1024 * 1024];
- int length;
- while ((length = in.read(buffer)) > 0) {
- out.write(buffer, 0, length);
- }
- in.close();
- out.close();
- } catch (FileNotFoundException ex) {
- ex.printStackTrace();
- } catch (IOException ex) {
- ex.printStackTrace();
- }
转:upload.parseRequest为空
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。