Page code:
<HTML>
<! -- Introduce relevant JS files and relative paths -->
<SCRIPT type = "text/JavaScript" src = "JS/jquery. js"> </SCRIPT>
<SCRIPT type = "text/JavaScript" src = "JS/Ajaxfileupload. Js "> </SCRIPT>
<! -- Execute the function for uploading files -->
<SCRIPT type = "text/JavaScript">
FunctionAjaxfileupload(){
$.Ajaxfileupload(
{
URL: 'Update. do? Method = Uploader ', // link to the server address
Secureuri: false,
Fileelementid: 'housemaps ', // ID attribute of the file selection box
Datatype: 'xml', // format returned by the server, which can be JSON
Success: function (data, status) // equivalent to the usage of the try statement block in Java
{
Certificate ('{result'{.html ('added successfully ');
},
Error: function (data, status, e) // equivalent to the Catch Block usage in Java
{
Certificate ('{result'{.html ('add failed ');
}
}
);
}
</SCRIPT>
</Head>
<Body>
<Form method = "Post" Action = "Update. do? Method = Uploader "enctype =" multipart/form-Data ">
<Input type = "file" id = "housemaps" name = "housemaps"/>
<Input type = "button" value = "Submit" onclick ="Ajaxfileupload() "/>
</Form>
<Div id = "result"> </div>
</Body>
</Html>
Server code:
Public class updateaction extends dispatchaction {
Public actionforward Uploader (actionmapping mapping, actionform form,
Httpservletrequest request, httpservletresponse response ){
Upformform = (upformform) form;
Formfile FF = upformform. gethousemaps ();
Try {
Inputstream is = ff. getinputstream ();
File file = new file ("D:/" + ff. getfilename (); // specify the file storage path and file name
Outputstream OS = new fileoutputstream (File );
Byte [] B = new byte [1024];
Int Len = 0;
While (LEN = is. Read (B ))! =-1 ){
OS. Write (B, 0, Len );
}
OS. Close ();
Is. Close ();
} Catch (exception e ){
E. printstacktrace ();
}
Return NULL;
}
}