$.ajaxFileUpload
(
{
url:'http://lh.abc.com:8080/gap/gap/fileUpload.do',//用于文件上傳的服務(wù)器端請(qǐng)求地址(本機(jī)為fxb.abc.com)
secureuri:false,//一般設(shè)置為false
fileElementId:'file',//文件上傳空間的id屬性 input type="file" id="file" name="file" />
dataType: 'jsonp',//返回值類型 一般設(shè)置為json
jsonp: 'jsoncallback',
jsonpCallback:'success_jsonpCallback',
function success_jsonpCallback(data) {
alert("1");
},
success: function (data, status) //服務(wù)器成功響應(yīng)處理函數(shù)
{
alert(data.message);//從服務(wù)器返回的json中取出message中的數(shù)據(jù),其中message為在struts2中action中定義的成員變量
if(typeof(data.error) != 'undefined')
{
if(data.error != '')
{
alert(data.error);
}else
{
alert(data.message);
}
}
},
error: function (data, status, e)//服務(wù)器響應(yīng)失敗處理函數(shù)
{
alert(status);
alert(e);
}
}
)
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 = "對(duì)不起,你上傳的文件格式不允許!!!";
} 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 = "上傳成功";
}
} catch (Exception e) {
e.printStackTrace();
message = "對(duì)不起,文件上傳失敗了!!!!";
}
return SUCCESS;
}
每次跨域上傳圖片時(shí),可以成功上傳到服務(wù)器上,但是不能正確的返回信息,總是進(jìn)入error方法中,正確應(yīng)該進(jìn)入success方法