我学习 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)
提前致谢,