我前两天出了个跨域问题,在入口函数上加了@Filters({@By(type=CrossOriginFilter.class)})这个注解就好了,但是我发现公司其他电脑访问时还是报错,报错如下:
跨域文件上传.html?_ijt=12dg1elorv6b1ia3633e8njf4:1 XMLHttpRequest cannot load http://10.2.1.102:8080/mychat/upload/image. Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://localhost:63342' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
但是我在我本地的webstrom里访问是没问题的,按道理讲都是跨域,应该没区别吧,可为何其他电脑就报错呢?我服务端日志:
2017-05-06 14:04:06,553 org.nutz.mvc.impl.UrlMappingImpl.get(UrlMappingImpl.java:101) DEBUG - Found mapping for [OPTIONS] path=/upload/image : UploadModule.image(UploadModule.java:48)
2017-05-06 14:04:06,554 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) DEBUG - Get 'uploadModule'<class com.mychat.controol.UploadModule>
2017-05-06 14:04:06,554 org.nutz.mvc.filter.CrossOriginFilter.match(CrossOriginFilter.java:49) DEBUG - Feedback -- [*] [get, post, put, delete, options] [origin, content-type, accept] [true]
2017-05-06 14:04:06,554 com.mychat.mvc.LogTimeProcessor.process(LogTimeProcessor.java:29) DEBUG - [OPTIONS]URI=/mychat/upload/image 1ms
我入口函数为:
@IocBean
@At("/upload")
@Ok("json")
public class UploadModule {
/**
* 发送图片,上传图片接口
* @param file
* @param context
* @return
*/
@At
@Filters({@By(type=CrossOriginFilter.class)})
@AdaptBy(type = UploadAdaptor.class, args = { "${app.root}/WEB-INF/tmp" })
public Object image(@Param("file") TempFile file,ServletContext context){
System.out.println(file.getName());
System.out.println(file.getMeta().getFileLocalName());
InputStream in = null;
OutputStream out = null;
File f = file.getFile();
// System.out.println("目录:"+context.getContextPath()); //获取项目名
// System.out.println("目录2:"+context.getRealPath("/"));
String tomcatPath =context.getRealPath("/");//获取到tomcat位于系统的绝对磁盘路径,//此为:D:\apache-tomcat-8.0.36\webapps\mychat\
tomcatPath = tomcatPath.substring(0,tomcatPath.length()-1);//此为:D:\apache-tomcat-8.0.36\webapps\mychat
tomcatPath = tomcatPath.substring(0,tomcatPath.lastIndexOf("\\"));//此为:D:\apache-tomcat-8.0.36\webapps
String relpath = tomcatPath+"\\upload\\"+file.getMeta().getFileLocalName(); // 此为: D:\\apache-tomcat-8.0.36\\webapps\\upload\\tomat.png
System.out.println("tomcatPath:"+tomcatPath);
// String relpath = context.getRealPath("upload")+"\\"+file.getMeta().getFileLocalName();
try {
in = new FileInputStream(file.getFile());
out = new FileOutputStream(relpath);
byte[] buf = new byte[1024];
int len = 0;
while((len = in.read(buf))!=-1){
out.write(buf,0,buf.length);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
String url ="/upload/"+file.getMeta().getFileLocalName(); //eclipse默认的tomcat目录是在其缓存文件中,你要自己指定到tomcat所在目录
System.out.println(url);
//构建json数据
Map<String,Object> data = new HashMap<String,Object>();
data.put("code", "0");
data.put("msg", "");
Map<String,String> sourceUrl = new HashMap<String,String>();
sourceUrl.put("src", url);
data.put("data", sourceUrl);
return data;
}
这当如何是好