java - 使用参数制作handleFileupload以共享方法

标签 java jsf primefaces managed-bean

我正在使用 fileuplod primefaces。我有 3 个按钮。每个按钮负责上传文件。我的第一个步骤是在我的 bean 上使用 3 种方法来上传每个文件。 有没有办法为所有类型制定相同的方法?每个文件都有自己的目录。

<h:form enctype="multipart/form-data"  style="height:125px;width:75px;">

                   <p:fileUpload  auto="true" 
                   fileUploadListener="#{composantbean.handleFileUpload(???,1)}"
                   sizeLimit="2097152"
                   label="Choose"
                   allowTypes="/(\.|\/)(pdf)$/"
                   description="Images"/> 
               </h:form>

在我的托管 bean 上,我正在考虑这个解决方案:

  public void handleFileUpload(FileUploadEvent event,int i) {
        String lienPDN =destination+"PDN\\"+FilenameUtils.getName(event.getFile().getFileName());
        File result = new File(lienPDN);
         try {
               FileOutputStream fileOutputStream = new FileOutputStream(result);
               byte[] buffer = new byte[BUFFER_SIZE];
               int bulk;
               InputStream inputStream = event.getFile().getInputstream();

               while (true) {
                   bulk = inputStream.read(buffer);
                   if (bulk < 0) {
                       break;
                       }

             fileOutputStream.write(buffer, 0, bulk);
             fileOutputStream.flush();
             }

          fileOutputStream.close();
          inputStream.close();
          FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName()+ " is uploaded.");
          FacesContext.getCurrentInstance().addMessage(null, msg);
          selcetitem.setLienPdn(lienPDN);
          } catch (IOException e) {

                        e.printStackTrace();
                        FacesMessage error = new FacesMessage(FacesMessage.SEVERITY_ERROR,"The files were not uploaded!", "");
                        FacesContext.getCurrentInstance().addMessage(null, error);
                        }      
         }

最佳答案

我认为更好的方法可能是实现三个 handleFileUpload()方法。每个都可以处理其独特的代码(例如传递正确的文件路径)。从那里您可以调用 private void wrapUpUpload(String path, (...)) .

最重要的是,这可以使您的代码保持可读性。如果还可以防止更改 handleFileUpload() 的默认实现的需要.

例如: 确保替换 1 , 2 , 3做一些有意义的事情

void handleFileUpload1(FileUploadEvent event) { 
    String path = "/uploads/1/";
    wrapUpUpload(path);
}

void handleFileUpload2(FileUploadEvent event) { 
    String path = "/uploads/2/";
    wrapUpUpload(path);
}

void handleFileUpload3(FileUploadEvent event) { 
    String path = "/uploads/3/";
    wrapUpUpload(path);
}

private void wrapUpUpload(String path, (...)) {
    // Upload the file
}

关于java - 使用参数制作handleFileupload以共享方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16354591/

相关文章:

java - 修改逻辑运算符真值表

jsf - PrimeFlex 未应用于 PrimeFaces

javascript - 打开对话框的多个实例

java - JSF:如何检查图像是否可以有效显示

java - PrimeFaces 数据表右键单击选择 + 行特定上下文菜单

java - 在 JSP EL 中使用变量访问 Map

java - 使用 JerseyIncation 构建器测试放置格式错误的 json

java - 检查 JTextField 字符

jsf - 识别和解决javax.el.PropertyNotFoundException:目标不可达

ajax - JSF 中的分组标记不呈现任何内容?