commons-fileupload上传附件api使用

jopen 9年前

boolean isMultipart = ServletFileUpload.isMultipartContent(request);  if (!isMultipart) {             goto.....              return;          }   // Create a new file upload handler  ServletFileUpload upload = new ServletFileUpload();  // Parse the request  FileItemIterator iter = upload.getItemIterator(request);  while (iter.hasNext()) {      FileItemStream item = iter.next();      String name = item.getFieldName();      InputStream stream = item.openStream();      if (item.isFormField()) {          System.out.println("Form field " + name + " with value "              + Streams.asString(stream) + " detected.");      } else {          System.out.println("File field " + name + " with file name "              + item.getName() + " detected.");          // Process the input stream          ...      }  }   具体上传服务器例子:  boolean isMultipart = ServletFileUpload.isMultipartContent(request);          if (!isMultipart) {              //goto              return;          }             DiskFileItemFactory dff = new DiskFileItemFactory();              try {              ServletFileUpload sfu = new ServletFileUpload(dff);              sfu.setSizeMax(10000*1024);              sfu.setHeaderEncoding("utf-8");              FileItemIterator fii = sfu.getItemIterator(request);              while (iterator.hasNext()) {                  fileStream = iterator.next();                   if (!fileStream.isFormField())                      break;                  fileStream = null;              }               if (fileStream == null) {                  return new BaseState(false, 7);  }             String savePath ="保存服务器路径";             String originFileName = fileStream.getName();              String suffix = FileType.getSuffixByFilename(originFileName);                 originFileName = originFileName.substring(0,                      originFileName.length() - suffix.length());              savePath = savePath + suffix;                 long maxSize = ((Long) conf.get("maxSize")).longValue();                 if (!validType(suffix, (String[]) conf.get("allowFiles"))) {                  return new BaseState(false, 8);              }               savePath = PathFormat.parse(savePath, originFileName);               String physicalPath = "路径" + savePath;               InputStream is = fileStream.openStream();               FileOutputStream out = new FileOutputStream(new File(physicalPath);               BufferedOutputStream output = new BufferedOutputStream(out);                Streams.copy(in, output, true);