damiean 2011-08-10 20:14
浏览 477
已采纳

List fileItems = upload.parseRequest(req);为空

都是说Struts2的拦截器给拦截了 但是我把改了之后 还是为空 请教各位!!

[code="java"]
public class FileUpload extends HttpServlet {

/**
 * Destruction of the servlet. <br>
 */
private String uploadPath = "E:\\save\\";
private File tempPath = new File("E:\\save\\tmp\\"); 

public void destroy() {
    super.destroy();
}

public void fileUpLoad(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException {
    res.setContentType("text/html; charset=utf-8");
    PrintWriter out = res.getWriter();
    System.out.println(req.getContentLength());
    System.out.println(req.getContentType());
    DiskFileItemFactory factory = new DiskFileItemFactory();
    factory.setSizeThreshold(4096);
    factory.setRepository(new File("E:\\save\\temp"));

    ServletFileUpload upload = new ServletFileUpload(factory);
    upload.setSizeMax(1000000);
    try {
        List fileItems = upload.parseRequest(req);
        Iterator iter = fileItems.iterator();

        // 正则匹配,过滤路径取文件名
        String regExp = ".+\\\\(.+)$";

        // 过滤掉的文件类型
        String[] errorType = { ".exe", ".com", ".cgi", ".asp" };
        Pattern p = Pattern.compile(regExp);
        while (iter.hasNext()) {
            FileItem item = (FileItem) iter.next();
            // 忽略其他不是文件域的所有表单信息
            if (!item.isFormField()) {
                String name = item.getName();
                long size = item.getSize();
                if ((name == null || name.equals("")) && size == 0)
                    continue;
                Matcher m = p.matcher(name);
                boolean result = m.find();
                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("E:\\save" + 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) {
        e.printStackTrace();
    } catch (FileUploadException e) {
        e.printStackTrace();
    }

[/code]
Action Code
[code="java"]

FileUpload f = new FileUpload();
HttpServletRequest req = ServletActionContext.getRequest();
HttpServletResponse res = ServletActionContext.getResponse();
f.fileUpLoad(req,res);

[/code]

web.xml
[code="java"]

struts2

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter



struts2
*.action

[/code]

jsp:
[code="java"]
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">




导入联系人
<br> function importE(){<br> var file = document.getElementById(&quot;file&quot;).value;<br> if( &quot;&quot; == file){<br> alert(&quot;请选择导入的文件!&quot;);<br> return false;</p> <pre><code> return true; } </code></pre> <p>
<body>
    <form id="importForm" method="post" enctype="multipart/form-data"  action="contactAction!importContact.action">
        <input type="file" name="file" id="file" />
        <input type="submit" name="Submit" value="导入" onclick="importE()" />
        ${msg}
        <c:remove var="msg" scope="session" />
    </form>
</body>

[/code]

请教各位 看下哪里错了 实在搞不出来了!!

  • 写回答

10条回答 默认 最新

  • laopeng301 2011-08-11 11:26
    关注

    那很简单啊···把这个做成一个统一的父action·

    比如像上面的
    /文件上传Action

    public class UploadAction extends ActionSupport {

    这里是上面代码···
    }

    如果Aaction需要这样的功能···
    这节 public class Aaction extends UploadAction {
    a自己的处理。。。。

    }
    b 需要

    public class Baction extends UploadAction

    不知道这样行不行哦··· 8)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(9条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵