コード例 #1
0
ファイル: Http.java プロジェクト: killdashnine/Play20
 /** Access a file part. */
 public FilePart getFile(String key) {
   for (FilePart filePart : getFiles()) {
     if (filePart.getKey().equals(key)) {
       return filePart;
     }
   }
   return null;
 }
コード例 #2
0
  public static Result upload() {
    MultipartFormData body = request().body().asMultipartFormData();
    FilePart input = body.getFile("inputFile");

    if (input != null) {
      String fileName = input.getFilename();
      String contentType = input.getContentType();
      File file = input.getFile();
      SeqFile seq = new SeqFile(file);
      return redirect(routes.Application.results(true));
    } else {
      flash("error", "Missing file");
      return redirect(routes.Application.results(false));
    }
  }
コード例 #3
0
ファイル: CosMultipartRequest.java プロジェクト: qzzsunly/cms
  /**
   * Constructs a new MultipartRequest to handle the specified request, saving any uploaded files to
   * the given directory, and limiting the upload size to the specified length. If the content is
   * too large, an IOException is thrown. This constructor actually parses the
   * <tt>multipart/form-data</tt> and throws an IOException if there's any problem reading or
   * parsing the request.
   *
   * <p>To avoid file collisions, this constructor takes an implementation of the FileRenamePolicy
   * interface to allow a pluggable rename policy.
   *
   * @param request the servlet request.
   * @param saveDirectory the directory in which to save any uploaded files.
   * @param maxPostSize the maximum size of the POST content.
   * @param encoding the encoding of the response, such as ISO-8859-1
   * @param policy a pluggable file rename policy
   * @exception IOException if the uploaded content is larger than <tt>maxPostSize</tt> or there's a
   *     problem reading or parsing the request.
   */
  public CosMultipartRequest(
      HttpServletRequest request,
      String saveDirectory,
      int maxPostSize,
      String encoding,
      FileRenamePolicy policy)
      throws IOException {
    // Sanity check values
    if (request == null) throw new IllegalArgumentException("request cannot be null");
    if (saveDirectory == null) throw new IllegalArgumentException("saveDirectory cannot be null");
    if (maxPostSize <= 0) {
      throw new IllegalArgumentException("maxPostSize must be positive");
    }

    // Save the dir
    File dir = new File(saveDirectory);

    // Check saveDirectory is truly a directory
    if (!dir.isDirectory()) throw new IllegalArgumentException("Not a directory: " + saveDirectory);

    // Check saveDirectory is writable
    if (!dir.canWrite()) throw new IllegalArgumentException("Not writable: " + saveDirectory);

    // Parse the incoming multipart, storing files in the dir provided,
    // and populate the meta objects which describe what we found
    MultipartParser parser = new MultipartParser(request, maxPostSize, true, true, encoding);

    // Some people like to fetch query string parameters from
    // MultipartRequest, so here we make that possible.  Thanks to
    // Ben Johnson, [email protected], for the idea.
    if (request.getQueryString() != null) {
      // Let HttpUtils create a name->String[] structure
      Map<String, String[]> queryParameters =
          RequestUtils.parseQueryString(request.getQueryString());
      // For our own use, name it a name->Vector structure
      for (Entry<String, String[]> entry : queryParameters.entrySet()) {
        parameters.put(entry.getKey(), Arrays.asList(entry.getValue()));
      }
    }

    Part part;
    while ((part = parser.readNextPart()) != null) {
      String name = part.getName();
      if (name == null) {
        throw new IOException("Malformed input: parameter name missing (known Opera 7 bug)");
      }
      if (part.isParam()) {
        // It's a parameter part, add it to the vector of values
        ParamPart paramPart = (ParamPart) part;
        String value = paramPart.getStringValue();
        List<String> existingValues = parameters.get(name);
        if (existingValues == null) {
          existingValues = new ArrayList<String>();
          parameters.put(name, existingValues);
        }
        existingValues.add(value);
      } else if (part.isFile()) {
        // It's a file part
        FilePart filePart = (FilePart) part;
        String fileName = filePart.getFileName();
        if (fileName != null) {
          filePart.setRenamePolicy(policy); // null policy is OK
          // The part actually contained a file
          filePart.writeTo(dir);
          files.put(
              name,
              new UploadedFile(
                  dir.toString(), filePart.getFileName(), fileName, filePart.getContentType()));
        } else {
          // The field did not contain a file
          files.put(name, new UploadedFile(null, null, null, null));
        }
      }
    }
  }