欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JSP使用ajaxFileUpload.js實現(xiàn)跨域問題

 更新時間:2016年04月15日 12:03:22   投稿:mrr  
這篇文章主要介紹了JSP使用ajaxFileUpload.js實現(xiàn)跨域問題的相關內容,本文介紹非常詳細,具有參考借鑒價值,感興趣的朋友一起學習吧

廢話不多說了,直接給大家貼代碼了。

jsp代碼如下

$.ajaxFileUpload 
( 
{ 
url:'http://lh.abc.com:8080/gap/gap/fileUpload.do',//用于文件上傳的服務器端請求地址(本機為fxb.abc.com) 
secureuri:false,//一般設置為false 
fileElementId:'file',//文件上傳空間的id屬性 <input type="file" id="file" name="file" /> 
dataType: 'jsonp',//返回值類型 一般設置為json 
jsonp: 'jsoncallback', 
jsonpCallback:'success_jsonpCallback', 
function success_jsonpCallback(data) { 
alert("1"); 
}, 
success: function (data, status) //服務器成功響應處理函數 
{ 
alert(data.message);//從服務器返回的json中取出message中的數據,其中message為在struts2中action中定義的成員變量 
if(typeof(data.error) != 'undefined') 
{ 
if(data.error != '') 
{ 
alert(data.error); 
}else 
{ 
alert(data.message); 
} 
} 
}, 
error: function (data, status, e)//服務器響應失敗處理函數 
{ 
alert(status); 
alert(e); 
} 
} 
)

配置文件

<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>

action中的處理如下

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 = "對不起,你上傳的文件格式不允許!!!";
} 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 = "對不起,文件上傳失敗了!!!!";
}
return SUCCESS;
}

每次跨域上傳圖片時,可以成功上傳到服務器上,但是不能正確的返回信息,總是進入error方法中,正確應該進入success方法

相關文章

最新評論