java - Making handleFileupload with parameter to share the methode -


i'm using fileuplod primefaces. have 3 buttons. every button responsible uploading file. first steep use 3 methods on bean upload every file. there way make same method types? every file has own directory.

<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> 

on managed bean, i'm thinking of solution:

  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()+ " uploaded.");           facescontext.getcurrentinstance().addmessage(null, msg);           selcetitem.setlienpdn(lienpdn);           } catch (ioexception e) {                          e.printstacktrace();                         facesmessage error = new facesmessage(facesmessage.severity_error,"the files not uploaded!", "");                         facescontext.getcurrentinstance().addmessage(null, error);                         }                } 

i think better way might implements 3 handlefileupload() methods. each can handle unique code (e.g. passing right filepath). there can call private void wrapupupload(string path, (...)).

most of keeps code readable. if prevents need changing default implementation of handlefileupload().

e.g.: make sure replace 1, 2, 3 meaningful

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 file } 

Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -