JSP使用ajaxFileUpload.js實現(xiàn)跨域問題
更新時間:2016年04月15日 12:03:22 投稿:mrr
這篇文章主要介紹了JSP使用ajaxFileUpload.js實現(xiàn)跨域問題的相關(guān)內(nèi)容,本文介紹非常詳細(xì),具有參考借鑒價值,感興趣的朋友一起學(xué)習(xí)吧
廢話不多說了,直接給大家貼代碼了。
jsp代碼如下
$.ajaxFileUpload
(
{
url:'http://lh.abc.com:8080/gap/gap/fileUpload.do',//用于文件上傳的服務(wù)器端請求地址(本機為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);
}
}
)
配置文件
<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;
}
每次跨域上傳圖片時,可以成功上傳到服務(wù)器上,但是不能正確的返回信息,總是進入error方法中,正確應(yīng)該進入success方法
您可能感興趣的文章:
- Ajax+Servlet+jsp顯示搜索效果
- 基于jsp的AJAX多文件上傳的實例
- jsp頁面 列表 展示 ajax異步實現(xiàn)方法
- AJAX和JSP混合使用方法實例
- jsp+ajax實現(xiàn)無刷新上傳文件的方法
- JSP+jquery使用ajax方式調(diào)用json的實現(xiàn)方法
- jsp+ajax實現(xiàn)的局部刷新較驗驗證碼(onblur事件觸發(fā)較驗)
- jquery ajax 如何向jsp提交表單數(shù)據(jù)
- jsp實現(xiàn)checkbox的ajax傳值實例
- jsp+ajax發(fā)送GET請求的方法
- 在(ASP/PHP/JSP/html/js)中禁止ajax緩存的方法集錦
- 使用js聲明數(shù)組,對象在jsp頁面中(獲得ajax得到j(luò)son數(shù)據(jù))
- jsp中利用jquery+ajax在前后臺之間傳遞json格式參數(shù)
- ajax 提交數(shù)據(jù)到后臺jsp頁面及頁面跳轉(zhuǎn)問題
相關(guān)文章
Jsp+Servlet實現(xiàn)文件上傳下載 文件上傳(一)
這篇文章主要為大家詳細(xì)介紹了Jsp+Servlet實現(xiàn)文件上傳下載中的第一部分文件上傳,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01
JSP數(shù)據(jù)分頁導(dǎo)出下載顯示進度條樣式
這篇文章主要介紹了JSP數(shù)據(jù)分頁導(dǎo)出下載顯示進度條樣式的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-12-12
JSP使用ajaxFileUpload.js實現(xiàn)跨域問題
這篇文章主要介紹了JSP使用ajaxFileUpload.js實現(xiàn)跨域問題的相關(guān)內(nèi)容,本文介紹非常詳細(xì),具有參考借鑒價值,感興趣的朋友一起學(xué)習(xí)吧2016-04-04
jsp使用ECharts動態(tài)在地圖上標(biāo)識點
echarts地圖展示功能很強大,官網(wǎng)上靜態(tài)展示的例子很多了,動態(tài)的資料少,需要參考本文的可以進來了解一下。2016-10-10

