No more nonsense to say, directly to everyone paste code.
The JSP code is as follows
$.ajaxfileupload
(
{
URL: ' http://lh.abc.com:8080/gap/gap/fileUpload.do ',// The server-side request address for file uploads (native to fxb.abc.com)
secureuri:false,//is generally set to False
Fileelementid: ' file ',//document upload Space id attribute < Input type= "file" id= "file" name= "file"/>
dataType: ' Jsonp ',//return value type is generally set to JSON
JSONP: ' Jsoncallback ',
Jsonpcallback: ' success_jsonpcallback ',
function success_jsonpcallback (data) {
alert ("1");
},
success:function (data, status)/Server Success Response handler function
{
alert (data.message);//Remove data from the message in the JSON returned from the server, where message is the member variable that is defined in the action in Struts2
if (typeof data.error)!= ' undefined ')
{
if (data.error!= ')
{
alert (data.error);
} else
{
alert (data.message);}}
,
error:function (data, status, e)/server response Failure Handler
{
alert (status);
Alert (e);
}
}
)
Configuration file
<action name= "FileUpload" class= "com.gap.action.FileUploadAction" method= "FileUpload" > <result "type="
JSON "name=" Success ">
<param name=" ContentType ">
text/html
</param>
</result >
<result type= "JSON" name= "error" >
<param name= "ContentType" >
text/html
</ param>
</result>
</action>
The action is handled as follows
Public String FileUpload () throws Exception {
string path = Servletactioncontext.getrequest (). Getrealpath ("/ Upload1 ");
String Path = Configdatainfo.getconfigvalue ("Imgserver");
try {
File f = this.getfile ();
if (This.getfilefilename (). EndsWith (". exe")) {Message
= "Sorry, the file format you uploaded does not allow!!!";
} else {
FileInputStream InputStream = new FileInputStream (f);
FileOutputStream outputstream = new FileOutputStream (path + "/"
+ this.getfilefilename ());
byte[] buf = new byte[1024];
int length = 0;
while (length = Inputstream.read (BUF))!=-1) {
outputstream.write (buf, 0, length);
}
Inputstream.close ();
Outputstream.flush ();
message = "Upload succeeded";
}
} catch (Exception e) {
e.printstacktrace ();
message = "Sorry, file upload failed!!!!";
}
return SUCCESS;
}
Upload a picture every time across the domain, you can successfully uploaded to the server, but not the correct return of information, always enter the error method, the correct should enter the Success method