废话不多说了,直接给大家贴代码了。jsp代码如下$.ajaxFileUpload ( { url:'http://lh.abc.com:8080/gap/gap/fileUpload.do',//用于文件上传的服务器端请求地址(本机为fxb....
废话不多说了,直接给大家贴代码了。
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方法
				 沃梦达教程
				
			本文标题为:JSP使用ajaxFileUpload.js实现跨域问题
				
        
 
            
        
             猜你喜欢
        
	     - Spring Security权限想要细化到按钮实现示例 2023-03-07
 - SpringBoot使用thymeleaf实现一个前端表格方法详解 2023-06-06
 - Java实现顺序表的操作详解 2023-05-19
 - 基于Java Agent的premain方式实现方法耗时监控问题 2023-06-17
 - ExecutorService Callable Future多线程返回结果原理解析 2023-06-01
 - Springboot整合minio实现文件服务的教程详解 2022-12-03
 - Java中的日期时间处理及格式化处理 2023-04-18
 - JSP页面间传值问题实例简析 2023-08-03
 - 深入了解Spring的事务传播机制 2023-06-02
 - JSP 制作验证码的实例详解 2023-07-30
 
						
						
						
						
						
				
				
				
				