Exemplo n.º 1
1
  /*
   * 测试getParameterMap
   */
  @Test
  public void testGetParameterMap_正常情况() throws Exception {
    byte[] bytes = content.getBytes("US-ASCII");
    HttpServletRequest request = new MockHttpServletRequest(bytes, CONTENT_TYPE);
    Map map = LazyParser.getParameterMap(request);
    // assertEquals("4",map.size()); //FIXME
    // 在form表单中的field重复的情况下,也应该能返回重复的表单数据
    FileItem f = (FileItem) map.get("file");
    assertEquals("This is the content of the file\n", new String(f.getBout().toByteArray()));
    assertEquals("text/whatever", f.getContentType());
    assertEquals("foo.tab", f.getFileName());

    f = (FileItem) map.get("field");
    assertEquals("fieldValue", f.getValue());

    f = (FileItem) map.get("multi");
    assertEquals("value1", f.getValue());
    LazyParser.release();
  }
Exemplo n.º 2
0
 /*
  * 测试getFileItemsFromRequest
  */
 @Test
 public void testGetFileItemFromRequest_正常情况() throws Exception {
   byte[] bytes = content.getBytes("US-ASCII");
   HttpServletRequest request = new MockHttpServletRequest(bytes, CONTENT_TYPE);
   List<FileItem> fileItem = LazyParser.getFileItemsFromRequest(request);
   assertEquals(1, fileItem.size());
   FileItem f = fileItem.get(0);
   assertEquals("This is the content of the file\n", new String(f.getBout().toByteArray()));
   assertEquals("text/whatever", f.getContentType());
   assertEquals("foo.tab", f.getFileName());
   LazyParser.release();
 }
Exemplo n.º 3
0
  /** 测试getParameter的正常情况 */
  @Test
  public void testGetParameter_正常情况_上传文件() throws Exception {
    byte[] bytes = content.getBytes("US-ASCII");
    HttpServletRequest request = new MockHttpServletRequest(bytes, CONTENT_TYPE);
    // 文件域
    Object o = LazyParser.getParameter(request, "file");
    assertTrue(o instanceof FileItem);
    FileItem f = (FileItem) o;
    assertEquals("This is the content of the file\n", new String(f.getBout().toByteArray()));
    assertEquals("text/whatever", f.getContentType());
    assertEquals("foo.tab", f.getFileName());
    // 普通域
    o = LazyParser.getParameter(request, "field");
    assertTrue(o instanceof FileItem);
    f = (FileItem) o;
    assertEquals("fieldValue", f.getValue());

    LazyParser.release();
  }
  /**
   * Handles the HTTP <code>POST</code> method.
   *
   * @param request servlet request
   * @param response servlet response
   * @throws ServletException if a servlet-specific error occurs
   * @throws IOException if an I/O error occurs
   */
  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    String id_book = "", len = "";

    // Imposto il content type della risposta e prendo l'output
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

    // La dimensione massima di ogni singolo file su system
    int dimensioneMassimaDelFileScrivibieSulFileSystemInByte = 10 * 1024 * 1024; // 10 MB
    // Dimensione massima della request
    int dimensioneMassimaDellaRequestInByte = 20 * 1024 * 1024; // 20 MB
    // Cartella temporanea
    File cartellaTemporanea = new File("C:\\tempo");

    try {
      // Creo un factory per l'accesso al filesystem
      DiskFileItemFactory factory = new DiskFileItemFactory();

      // Setto la dimensione massima di ogni file, opzionale
      factory.setSizeThreshold(dimensioneMassimaDelFileScrivibieSulFileSystemInByte);
      // Setto la cartella temporanea, Opzionale
      factory.setRepository(cartellaTemporanea);

      // Istanzio la classe per l'upload
      ServletFileUpload upload = new ServletFileUpload(factory);

      // Setto la dimensione massima della request, opzionale
      upload.setSizeMax(dimensioneMassimaDellaRequestInByte);

      // Parso la riquest della servlet, mi viene ritornata una lista di FileItem con
      // tutti i field sia di tipo file che gli altri
      List<FileItem> items = upload.parseRequest(request);

      /*
       *
       * Giro per tutti i campi inviati
       */
      for (int i = 0; i < items.size(); i++) {
        FileItem item = items.get(i);

        // Controllo se si tratta di un campo di input normale
        if (item.isFormField()) {
          // Prendo solo il nome e il valore
          String name = item.getFieldName();
          String value = item.getString();
          if (name.equals("id_book")) {
            id_book = value;
          }
          if (name.equals("len")) {
            len = value;
          }
        }
        // Se si stratta invece di un file ho varie possibilità
        else {
          // Dopo aver ripreso tutti i dati disponibili
          String fieldName = item.getFieldName();
          String fileName = item.getName();
          String contentType = item.getContentType();
          boolean isInMemory = item.isInMemory();
          long sizeInBytes = item.getSize();

          // scrivo direttamente su filesystem

          File uploadedFile =
              new File("/Users/Babol/Desktop/dVruhero/aMuseWebsite/web/userPhoto/" + fileName);
          // Solo se veramente ho inviato qualcosa
          if (item.getSize() > 0) {
            item.write(uploadedFile);
            DBconnection.EditCoverBook(fileName, Integer.parseInt(id_book));
          }
        }
      }

      // out.println("</body>");
      // out.println("</html>");

    } catch (Exception ex) {
      System.out.println("Errore: " + ex.getMessage());
    } finally {
      if (len.equals("ita")) response.sendRedirect("modify_book.jsp?id_book=" + id_book);
      else response.sendRedirect("modify_book_eng.jsp?id_book=" + id_book);
    }
  }