基本的にtype="file"
、サーバーに画像を送信するサーバーがあります。サーバーでは、画像サイズについてクライアントにエラーを送信する場合、画像サイズが1MBを超えていないことを確認して、クライアントがユーザーにエラーをポップできるようにする必要があります。
これが私のサーブレットスクリプトです:
@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")
が、クライアントはそれを受信しません.
誰かが私が間違っていることを教えてくれたら本当に感謝しています:)