1

I have an upload routine which is working for servlets. Now I was trying to put this routine in a jsf managed bean like this:

 public void uploadFile() throws IOException, ServletException{

    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
    HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();


    Part filePart = request.getPart("item");
    String filename = getFilename(filePart);
    InputStream filecontent = filePart.getInputStream();

    //persist the data here

}

when trying to run it on the server, of course there is the error message: "PWC4016: Request.getPart is called without multipart configuration. Either add a @MultipartConfig to the servlet, or a multipart-config element to web.xml"

But I don't know where to put this annotation, neither do I have the name of the jsf generated servlet of my managed bean so I can't put it into the web.xml neither. Is it generally a bad idea to put this routine into a managed bean or should I stick to the servlet variant?

nico1510
  • 606
  • 9
  • 29

1 Answers1

1

The @MultipartConfig basically needs to be put on the FacesServlet. You can't do it yourself, but this is already done for the upcoming JSF 2.2, complete with a new standard <h:inputFile> component with ajax support, see also JSF spec issue 802.

Until then, your best bet is grabbing a 3rd party component library or homebrewing a custom component.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • currently I'm using JUpload Applet for upload files, so uploading is not the problem. It's rather that processing the upload with a managed beans seems not possible at the moment – nico1510 Oct 12 '12 at 10:25
  • If the source is not a JSF view, why do you need a JSF model then? Indeed just use a simple servlet then. Even with JSF 2.2 it would not be (nicely) possible. – BalusC Oct 12 '12 at 10:27
  • Since I have a file downloadlink in my jsf and a managedbean which serves the file to download (according to your post at [link](http://stackoverflow.com/a/3428207/1644061) ) I thought it would be a good idea to put the upload routine also in this bean. But I think thats the wrong decision right? – nico1510 Oct 12 '12 at 11:31
  • Depends on whether JSF is absolutely mandatory to serve the file. For example, you need to download the file by a JSF POST request instead of a simple GET request because there are some input fields involved for which you'd like to perform JSF validation and so on. – BalusC Oct 12 '12 at 11:37