基本上我有一个type="file"
将图像发送到服务器,在服务器中,如果它向客户端发送有关图像大小的错误,我需要检查图像大小是否不高于 1MB,以便客户端可以向用户弹出错误。
这是我的 servlet 脚本:
@MultipartConfig(
maxFileSize=1024*1024 // 1Mb max
)
public class ProPicUploader extends HttpServlet {
private static final long serialVersionUID = 1L;
public void init() throws ServletException
{
System.out.println("Initiate method is called in " + this.getClass().getName());
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
try{
MultipartRequest multipartRequest = new MultipartRequest(request, "D:\\");
} catch(IOException e){
out.print("Image size is bigger than 1MB");
System.out.println(e.getMessage());
}
out.print("Successfully Uploaded");
}
public void destroy(){
System.out.println("Destory method is called in: " + this.getClass().getName());
}
}
客户端脚本:
if(formdata){
$.ajax({
url: '../propicuploader',
type: 'POST',
data: formdata,
processData: false,
contentType: false,
success: function(data){
alert(data);
if(data == "0x00000035"){
$("#NotPictureerror_spn").text("File size is too big, Please select a file upto 2MB");
}
}
error: function(data){
alert(data);
}
});
}
和我的 web.xml 脚本:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Duck</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>ProPicUploader</servlet-name>
<servlet-class>/ProPicUploader</servlet-class>
</servlet>
</web-app>
到目前为止,如果图像大小低于 1MB,它会显示,Successfully Uploaded
但如果图像大小高于 1MB,它不会弹出任何内容,尽管我输入了发送错误消息,out.print("Image size is bigger than 1MB")
但我的客户永远不会收到它。
如果有人告诉我我做错了什么,我真的很感激:)