我已经自己解决了这个问题
贴出解决方案,我在这的使用场景是 只需要取得 包含文件二进制的POST form表单的 token字段
我的解决方案
@IocBean
public class WxAppFilter implements ActionFilter{
@Inject
Dao dao;
@Override
public View match(ActionContext actionContext) {
HttpServletRequest request = actionContext.getRequest();
String token = request.getParameter("token");
if (request.getMethod().equals("POST") && token == null){
DiskFileItemFactory factory = new DiskFileItemFactory();
String repositoryPath = request.getSession().getServletContext().getRealPath("/upload/temp");
factory.setRepository(new File(repositoryPath));
factory.setSizeThreshold(1024 * 1024);
ServletFileUpload uploader = new ServletFileUpload(factory);
ArrayList<FileItem> list = null;
try {
list = (ArrayList<FileItem>) uploader
.parseRequest(request);
System.out.println(list.size());
for (FileItem fileItem : list) {
if (fileItem.isFormField()) {
//处理非二进制字段
String name = fileItem.getFieldName();
String value = fileItem.getString();
if (name.equals("token")){
token = value;
}
}else {
//处理二进制文件信息
// String value = fileItem.getName();
// // value.lastIndexOf("\\")返回“\\”最后出现的位置下标
// int start = value.lastIndexOf("\\");
// // substring 截取字符串方式之一
// String fileName = value.substring(start + 1);
// // 将其中包含的内容写到path()下,即upload目录下,名为fileName的文件中
// fileItem.write(new File(path, fileName));
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}
}
}
}