0

我学习 JavaEE,并尝试创建一个包含 2 个字段(电子邮件、图像)的简单网页。我想验证我的电子邮件地址和图片。

我的代码是:

测试.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>Send picture</title>
     <!-- <link type="text/css" rel="stylesheet" href="<c:url value="/inc/form.css"/>" /> -->
    </head>
    <body>
        <form method="POST" action="upload" enctype="multipart/form-data" >
            <fieldset>
                <legend>Send picture</legend>
                <label for="email">email <span class="requis">*</span></label>
                <input type="text" id="email" name="email" value="" size="20" maxlength="60" />
                <br />
                <label for="fichier">Your picture <span class="requis">*</span></label>
                <input type="file" id="imageClient" name="imageClient" />
                <br />
                 <input type="submit" value="Envoyer" class="sansLabel" />
                <br />        
            </fieldset>
        </form>
    </body>
</html>

我的上传.java

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class upload  extends HttpServlet
{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public static final String EMAIL = "email";
    public static final String PICTURE = "imageClient";
    public static final String VUE= "/WEB-INF/upload.jsp";

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.getServletContext().getRequestDispatcher(VUE).forward(request, response);
        System.out.println("[+] doGet !!!!");
        String email = request.getParameter(EMAIL);
        System.out.println(email);

    }



 public void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException
 {
     System.out.println("[+] doPost  !!!!");

     String email = request.getParameter(EMAIL);
    try {
        System.out.println(email);
        validationEmail(email);
        validationImage(PICTURE);
    } catch (Exception e) {
        System.out.println("[-] Error: " + e);
        }
    }

        private void validationEmail( String email ) throws Exception 
        {
            if ( email != null && email.trim().length() != 0 )
            {
                if ( !email.matches( "([^.@]+)(\\.[^.@]+)*@([^.@]+\\.)+([^.@]+)" ) )
                {
                    System.out.println("[-] bad mail :(");
                    throw new Exception( "Merci de saisir une adresse mail valide." );
                }
                else
                {
                    System.out.println("[+]good mail :)");
                }
            } 
            else 
            {
                System.out.println("[-]empty mail :(");
                throw new Exception( "Merci de saisir une adresse mail." );
            }
        }

        private void validationImage( String picture ) throws Exception 
        {

        }
}

我的问题是: - 在 doPost 中,我的字符串电子邮件是空的。如何解决我的错误?-如何恢复图片以及如何验证图片是否为好图片(.jpeg、jpg、png)

提前致谢,

4

1 回答 1

1

- 在 doPost 中,我的字符串电子邮件是空的。如何解决我的错误?

您的电子邮件不应为空。看看这个例子。这将对您有所帮助。

-如何恢复图片以及如何验证图片是否为好图片(.jpeg、jpg、png)

要验证好图片,请编写如下方法:

private boolean  isFileNameValid(final Part part) {
final String partHeader = part.getHeader("content-disposition");
String fileName="";
for (String content : part.getHeader("content-disposition").split(";")) {
    if (content.trim().startsWith("filename")) {
        fileName=content.substring(
                content.indexOf('=') + 1).trim().replace("\"", "");
    }
 }
  if(fileName.contains(".jpg")){// Similarly for all extension type.
    return true;
 }
 else{
    return false
 }
}
于 2013-11-15T10:31:34.740 回答