你用了webwork,就不用自己写这些了啊!!直接在action里面getFiles就可以了。

解决方案 »

  1.   

    你用webwork,
    在webwork.properties里面定义webwork.multipart.parser=jakarta
    webwork.multipart.maxSize=2097152注意,这里需要引入jakarta的几个包:
        * jakarta commons io
        * jakarta commons codec
        * jakarta commons fileupload
        * jakarta commons loggingxwork.xml定义action
    <action name="doUpload" class="com.examples.UploadAction">
        <interceptor-ref name="fileUpload"/>
        <interceptor-ref name="basicStack"/>
        <result name="success">success.jsp</result>
    </action>jsp里面的代码:
      <ww:form action="doUpload" method="post" enctype="multipart/form-data">
          <ww:file name="upload" label="File"/>
          <ww:submit/>
      </ww:form>java代码:   public com.examples.UploadAction implemements Action {
          private File file;
          private String contentType;
          private String filename;      public void setUpload(File file) {
             this.file = file;
          }      public void setUploadContentType(String contentType) {
             this.contentType = contentType;
          }      public void setUploadFileName(String filename) {
             this.filename = filename;
          }      ...
     }
      

  2.   

    webwork是nb的,根本不需要你写那些,你要做的就是写配置文件,
    然后定义getter,setter就行了。如果我的回答解决了问题,欢迎浏览:http://www.jopener.cn
    或者
    http://www.jopener.com/fun
      

  3.   

    謝謝大家~!特別是masse但是我想知道~!你的代碼裏好像沒有上傳的代碼吧~~~能不能貼個完整的代碼來給偶看下呢?
      

  4.   

    你用webwork不用写上传代码,我给你的部分就是完整的。
    看来你没有明白webwork...public void setUpload(File file) {
    this.file = file;
    }这个方法可以直接接收来自客户端上传的文件,会自动给你写好你在webwork.properties里面写好
    webwork.multipart.parser=jakarta
    webwork.multipart.saveDir=E:/temp
    webwork.multipart.maxSize=10097152第一个告诉webwork,是使用commons-upload组件
    第二个是上传的临时目录
    第三个是上传文件的最大尺寸按照我上面的写就行了,我给的就是完整的代码。不需要其它任何东西
      

  5.   

    謝謝masse,但是我還不懂,我把我的代碼貼出來吧:頁面:<form action="test!upload.action" enctype="multipart/form-data" method="POST">
    <input type="text" name="filesFileName"/> <input type="file" name="files"/><br>
       <input type="text" name="filesFileName"/> <input type="file" name="files"/><br>
        <input type="text" name="filesFileName"/> <input type="file" name="files"/><br>
        <input type="text" name="filesFileName"/> <input type="file" name="files"/><br>
        <input type="submit" name="上傳"/>
    </form>
    action如下:
    File file;
        String fileContentType;
        String fileFileName;
        File[] files;
        String[] filesContentType;
        String[] filesFileName;
        private boolean debug=true; public String upload() throws Exception{

    ActionContext context=ActionContext.getContext();
            Map map=context.getParameters();
            context.getValueStack();
            if(filesFileName!=null)
            {
                for (int i = 0; i < filesFileName.length; i++) {
                    String srcName = filesFileName[i];
                    File target = new File("F:/" + srcName);
                    if (target.exists()) {
                        target.delete();
                    }
                    //files[i].renameTo(target);//不知道為什麼files這個對象總是為null,有set/get方法吖~
                }
            }
    return "edit";
    }
    public boolean isDebug() {
    return debug;
    } public void setDebug(boolean debug) {
    this.debug = debug;
    } public File getFile() {
    return file;
    } public void setFile(File file) {
    this.file = file;
    } public String getFileContentType() {
    return fileContentType;
    } public void setFileContentType(String fileContentType) {
    this.fileContentType = fileContentType;
    } public String getFileFileName() {
    return fileFileName;
    } public void setFileFileName(String fileFileName) {
    this.fileFileName = fileFileName;
    } public File[] getFiles() {
    return files;
    } public void setFiles(File[] files) {
    this.files = files;

    if(files==null)
            {
                return ;
            }
            if(debug)
            {
                for (int i = 0; i < files.length; i++) {
                    System.out.println(files[i].getName()) ;
                }
            }
    } public String[] getFilesContentType() {
    return filesContentType;
    } public void setFilesContentType(String[] filesContentType) {
    this.filesContentType = filesContentType;
    } public String[] getFilesFileName() {
    return filesFileName;
    } public void setFilesFileName(String[] filesFileName) {
    this.filesFileName = filesFileName;
    }
    如果不去掉files[i].renameTo(target);這行就會報null,如果去掉這行,就會出現如下消息:2007-08-30 16:59:36,327 WARN [com.opensymphony.webwork.dispatcher.multipart.MultiPartRequest] - Item is a file upload of 0 size, ignoring
    2007-08-30 16:59:36,327 WARN [com.opensymphony.webwork.dispatcher.multipart.MultiPartRequest] - Item is a file upload of 0 size, ignoring
    2007-08-30 16:59:36,327 WARN [com.opensymphony.webwork.dispatcher.multipart.MultiPartRequest] - Item is a file upload of 0 size, ignoring
    2007-08-30 16:59:39,436 ERROR [com.opensymphony.webwork.interceptor.FileUploadInterceptor] - Content-Type not allowed: files "upload_14e81a4b_114b5feb16e__7ffd_00000001.tmp" text/plain
    2007-08-30 16:59:48,155 INFO [com.opensymphony.webwork.views.freeer.FreeerManager] - Instantiating Freeer ConfigManager!, com.opensymphony.webwork.views.freeer.FreeerManager
    2007-08-30 16:59:52,999 INFO [com.opensymphony.webwork.interceptor.FileUploadInterceptor] - Removing file files \tmp\upload_14e81a4b_114b5feb16e__7ffd_00000001.tmp
      

  6.   

    好像没有别的办法,因为webwork总是在dispatch里自动做multipart的处理。不过这里有个“不正规”的办法,不知道是否适合;想法就是让webwork找不到multipart解析器,这样就可以自己做解析了。方法是:修改webwork.properties,将webwork.multipart.parser设置成非pell、非cos、非jakata的任意不存在的解析器这样做可能有些负面作用,没检查过。