JSP uses ajaxfileupload.js to implement cross domain problem _jsp programming

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.