老师请教哈这个问题:
部分代码:if (!item.isFormField()) {
String name = item.getName();
long size = item.getSize();

if ((name == null || name.equals("")) && size == 0)
continue;
Matcher m = p.matcher(name);
out.println("m="+m);
boolean result = m.find();
out.println("result="+result);
if (result) {
for (int temp = 0; temp < errorType.length; temp++) {
if (m.group(1).endsWith(errorType[temp])) {
throw new IOException(name + ": wrong type");
}
为什么m有值,result=false;啊?难道boolean result = m.find();find()
这个方法有问题
。。还是有新方法boolean

解决方案 »

  1.   

    m只是个匹配器,LZ可以看看Matcher 的API帮助文档,find()方法会返回这个匹配器,在目标字符串中,有没有匹配到正则式表达的字符串。没匹配到,自然为false了。LZ可以把p初始化那段代码,还有name是什么字符串,这两段代码贴出来看看。
      

  2.   

    name=IMG_20120927_194420.jpg 
    size=273815 
    m=java.util.regex.Matcher[pattern=.+\\(.+)$ region=0,23 lastmatch=] 
    result=false 
    java.io.IOException: fail to upload 
    // 正则匹配,过滤路径取文件名
    String regExp = ".+\\\\(.+)$";
      

  3.   

    public void doPost(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {

    res.setContentType("text/html; charset=GB2312");
    PrintWriter out = res.getWriter();
    System.out.println(req.getContentLength());
    System.out.println(req.getContentType());
    DiskFileItemFactory factory = new DiskFileItemFactory();
    // maximum size that will be stored in memory
    factory.setSizeThreshold(4096);
    // the location for saving data that is larger than getSizeThreshold()
    factory.setRepository(new File("d:\\temp\\")); ServletFileUpload upload = new ServletFileUpload(factory);
    // maximum size before a FileUploadException will be thrown
    upload.setSizeMax(1000000);
    try {
    List fileItems = upload.parseRequest(req);
    // assume we know there are two files. The first file is a small
    // text file, the second is unknown and is written to a file on
    // the server
    Iterator iter = fileItems.iterator(); // 正则匹配,过滤路径取文件名
    String regExp = ".+\\\\(.+)$"; // 过滤掉的文件类型
    String[] errorType = { ".exe", ".com", ".cgi", ".jsp" };
    Pattern p = Pattern.compile(regExp);
    while (iter.hasNext()) {
    FileItem item = (FileItem) iter.next();
    /*if(item.isFormField()) {
    if(item.getFieldName().equals("id")) {
    id = Integer.parseInt(item.getString());

    }
    }*/
    // 忽略其他不是文件域的所有表单信息
    if (!item.isFormField()) {
    String name = item.getName();
    long size = item.getSize();
    out.println("name="+name);
    out.println("size="+size);
    if ((name == null || name.equals("")) && size == 0)
    continue;
    Matcher m = p.matcher(name);
    out.println("m="+m);
    boolean result = m.find();
    out.println("result="+result);
    if (result) {
    for (int temp = 0; temp < errorType.length; temp++) {
    if (m.group(1).endsWith(errorType[temp])) {
    throw new IOException(name + ": wrong type");
    }
    }
    try { // 保存上传的文件到指定的目录 // 在下文中上传文件至数据库时,将对这里改写
    item.write(new File("d:\\" + m.group(1))); out.print(name + "&nbsp;&nbsp;" + size + "<br>");
    } catch (Exception e) {
    out.println(e);
    } } else {
    throw new IOException("fail to upload");
    }
    }
    }
    } catch (IOException e) {
    out.println(e);
    } catch (FileUploadException e) {
    out.println(e);
    }
      

  4.   

    那到底怎么解决啊?我的name只是文件名字,不带路径的啊??????