上传是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= "上传文件扩展名是不允许的扩展名。 只允许" + 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();
- }