日期:2014-05-20  浏览次数:20874 次

UploadForm上传一个form中的多个file
先上图


在一个form里面实现两个文件的不同时上传,两个上传互不干涉.

之前只有一个file 和submit,即Select Excel File那一行;现在我加上了下面的Feedback File 一行file和submit.
结果导致只能Feedback这个submit能够上传文件,Select那一行的submit一上传就出错,说是The input file was not found.
然后我后台输出Select一行的file.getFileName()为空.这是为什么呢!

上代码

这是form体
HTML code
<html:form action="uploadAction.do?method=upFile"
            enctype="multipart/form-data">
            <c:choose>
                <c:when test="${zn == 'pilot'}">
                    <input type="radio"  name="name" value="pilot" 
                        checked="checked"  onclick="getZoneDate('pilot');"/>
                    <font color="red">PRFQ1</font>
                </c:when>
                <c:otherwise>
                    <input type="radio"  name="name" value="pilot" onclick="getZoneDate('pilot');"/>
                    <font color="red">PRFQ1</font>
                </c:otherwise>
            </c:choose>
            <c:choose>
                <c:when test="${zn == 'week'}">
                    <input type="radio"  name="name" value="week"
                        checked="checked" onclick="getZoneDate('week');"/>
                    <font color="red">PRFQ2</font>
                </c:when>
                <c:otherwise>
                    <input type="radio"  name="name" value="week" onclick="getZoneDate('week');"/>
                    <font color="red">PRFQ2</font>
                </c:otherwise>
            </c:choose>
            <br/>
                Select Excel File:<html:file property="theFile" />&nbsp;&nbsp;
                <html:submit value="upload"/>&nbsp;&nbsp;
            <br/>
            FeedBack &nbsp;&nbsp; File:<html:file property="theFile" />&nbsp;&nbsp;
            <html:submit value="upload" onclick="this.form.action='uploadAction.do?method=upFBFile'"/>&nbsp;&nbsp;
        </html:form>


这是后台action过程

Select一行的submit
Java code
public ActionForward upFile(ActionMapping mapping, ActionForm form,
                HttpServletRequest request, HttpServletResponse response)
                throws Exception {
            String filePathName = "";
            if (form instanceof UploadForm) {
                String encoding = request.getCharacterEncoding();

                if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8"))) {
                    response.setContentType("text/html; charset=gb2312");
                }
                UploadForm theForm = (UploadForm) form;
                FormFile file = theForm.getTheFile();
                String contentType = file.getContentType();
                String zoneName = theForm.getName();
                String size = (file.getFileSize() + " bytes");
                String fileName = file.getFileName();
                // 
                System.out.println("fileName="+fileName);  //这里打印为"fileName=",明显没有值
                String timeStr = df.getTime("yyyyMMddhhmmss");
                try {
                    InputStream stream = file.getInputStream();
                    String filePath = request.getRealPath("/");
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    OutputStream bos = null;
                    filePathName = filePath+ "files" + "\\"+ timeStr+file.getFileName();
                    String pathName = request.getSession().getServletContext().getRealPath("/") + "files";
                    File files= new File(pathName);
                    if(files.exists()&&files.isDirectory()){
                        bos = new FileOutputStream(filePath + "files"+ "\\"
                                + timeStr+file.getFileName());
                        int bytesRead = 0;
                        byte[] buffer = new byte[8192];
                        while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
                            bos.write(buffer, 0, bytesRead);
                        }
                    }else{
                        boolean flag = files.mkdir();
                        if(flag){
                            bos = new FileOutputStream(filePath + "files"+ "\\"
                                    + timeStr+file.getFileName());
                            int bytesRead = 0;
                            byte[] buffer = new byte[8192];
                            while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
                                bos.write(buffer, 0, bytesRead);
                            }
                        }
                    }
                    bos.close();
                    stream.close();
                } catch (Exception e) {
                }
                request.setAttribute("contentType", contentType);
                request.setAttribute("size", size);
                request.setAttribute("fileName", fileName);
                request.setAttribute("filePathName", filePathName);
                request.setAttribute("zoneName", zoneName);
                return mapping.findForward("display");
            }
            return null;
        }