我页面用Web Uploader进行图片上传,后台使用一个过滤器解决跨域的options问题,然后我给入口类加上了这个过滤器注解配置,但是无效
页面代码:
<body>
<div id="uploader-demo">
<!--用来存放item-->
<div id="fileList" class="uploader-list"></div>
<div id="filePicker">选择图片</div>
</div>
<script>
// 初始化Web Uploader
var uploader = WebUploader.create({
// 选完文件后,是否自动上传。
auto: true,
withCredentials: true, // 支持CORS跨域带cookie
// 文件接收服务端。
server: 'http://127.0.0.1:8080/mychat/upload/image',
// 选择文件的按钮。可选。
// 内部根据当前运行是创建,可能是input元素,也可能是flash.
pick: '#filePicker',
// 只允许选择图片文件。
accept: {
title: 'Images',
extensions: 'gif,jpg,jpeg,bmp,png',
mimeTypes: 'image/*'
}
});
</script>
入口类代码:
package com.mychat.controol;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.AdaptBy;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;
import org.nutz.mvc.upload.TempFile;
import org.nutz.mvc.upload.UploadAdaptor;
import com.mychat.filter.PassHttpFilter;
@IocBean
@At("/upload")
@Ok("json")
@Filters(@By(type=PassHttpFilter.class))
public class UploadModule {
/**
* 发送图片,上传图片接口
* @param file
* @param context
* @return
*/
@At
@POST
@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();
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 = "/mychat/upload/"+file.getMeta().getFileLocalName(); //eclipse默认的tomcat目录是在其缓存文件中,你要自己指定到tomcat所在目录
//构建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;
}
@At
public void test(){
System.out.println("lalala");
}
}
Filter代码:
package com.mychat.filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.nutz.mvc.ActionContext;
import org.nutz.mvc.ActionFilter;
import org.nutz.mvc.View;
public class PassHttpFilter implements ActionFilter {
@Override
public View match(ActionContext actionContext) {
System.out.println("execute passHttpFilter...");
HttpServletResponse res = actionContext.getResponse();
HttpServletRequest request = actionContext.getRequest();
res.setContentType("textml;charset=UTF-8");
res.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
res.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
res.setHeader("Access-Control-Max-Age", "0");
res.setHeader("Access-Control-Allow-Headers", "Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With,userId,token");
res.setHeader("Access-Control-Allow-Credentials", "true");
res.setHeader("XDomainRequestAllowed","1");
return null;
}
}
页面进行文件上传后,控制台打印了这句话:
2017-05-04 16:11:30,016 org.nutz.mvc.impl.ActionInvoker.getActionChain(ActionInvoker.java:87) DEBUG - Path=[/upload/image] available methods[POST] but request [OPTIONS], using the wrong http method?
2017-05-04 16:11:30,017 org.nutz.mvc.impl.UrlMappingImpl.get(UrlMappingImpl.java:110) DEBUG - Search mapping for [OPTIONS] path=/upload/image : NOT Action match
这个意思就是没走filter,但为何没走呢? 还有我在主模块上加上这个过滤器也试过,但是还没用